【发布时间】:2019-09-12 22:01:30
【问题描述】:
我正在尝试使用 apply 函数将行子集的所有值复制到新列中,但它似乎只是复制了整个数据框范围。结果,我收到了数据帧的该子集,尽管我希望 df.loc[index, 'consolidated_commentary'] 包含 all_commentary_columns 中包含的列中文本的串联版本
我的代码是:
for index, row in df[all_commentary_columns].iterrows():
if pd.isna(row).prod():
df.loc[index, 'new_col'] = 'good'
else:
df.loc[index, 'new_col'] = 'bad'
df.loc[index, 'consolidated_commentary'] = df[all_commentary_columns].apply(lambda x: x.loc[all_commentary_columns], axis=1)
【问题讨论】:
-
.loc[index]中的index是什么?请提供您的示例数据和预期输出。 -
@QuangHoang 更新了!
标签: python-3.x pandas