【发布时间】:2022-01-13 23:52:13
【问题描述】:
我有一个空数据框,当它通过循环时,返回与列表中最后一个值的条件匹配的结果,而不是考虑所有的值。
数据框被替换为列表中的每个条件,而不是被添加到其中。
我试过empty_df.append(...),还是不行。
njobs = ['doc', 'phy', 'sci', 'math', 'stat']
empty_df = pd.DataFrame()
for i in njobs:
df_new = df.loc[df['title'] == i]
empty_df['skills'] = df_new[['(Level 6)','(Level 5)',
'(Level 4)','(Level 3)', '(Level 2)',
'(Level 1)',
'(Level 0)']]
empty_df['cat_id'] = df_new[['(L6)','(L5)','(L4)','(L3)',
'(L2)','(L1)', 'ID']]
empty_df['wt'] = df_new[['Weight']]
empty_df['job'] = i
【问题讨论】:
标签: python pandas dataframe iteration