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
添加回答
举报