1 回答

TA貢獻1856條經驗 獲得超5個贊
這是相當簡單的
您可以使用數據框方法iterrows來迭代您的行,然后處理單行以提取您的知識。為了完整起見,我生成了一個示例數據框來演示該行為:
import pandas as pd
import random
# here I am generating my dataframe similiar to yours
list_of_dicts = [{'Q'+str(i)+'.':random.randrange(10) for i in range(1, 8)} for j in range(5)]
index = ['Lotte', 'Lara', 'Linda', 'Tom', 'jantje']
df = pd.DataFrame(list_of_dicts)
df.index = index
# here you can see the df structure
print(df)
# here the algorithm
for row in df.iterrows():
name = row[0]
print("For " + name)
for key in row[1].keys():
if row[1][key] < 5:
print("for {} it is Lower than 5".format(key))
我的數據框:
Q1. Q2. Q3. Q4. Q5. Q6. Q7.
Lotte 6 8 5 8 8 1 6
Lara 1 7 0 7 5 5 1
Linda 6 6 0 3 9 7 4
Tom 5 8 2 5 3 8 3
jantje 5 5 9 9 5 0 3
我的輸出:
For Lotte
for Q6. it is Lower than 5
For Lara
for Q1. it is Lower than 5
for Q3. it is Lower than 5
for Q7. it is Lower than 5
For Linda
for Q3. it is Lower than 5
for Q4. it is Lower than 5
for Q7. it is Lower than 5
For Tom
for Q3. it is Lower than 5
for Q5. it is Lower than 5
for Q7. it is Lower than 5
For jantje
for Q6. it is Lower than 5
for Q7. it is Lower than 5
添加回答
舉報