Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 熊猫:使用“应用”将特定列中的行值复制到新列中_Python 3.x_Pandas - Fatal编程技术网

Python 3.x 熊猫:使用“应用”将特定列中的行值复制到新列中

Python 3.x 熊猫:使用“应用”将特定列中的行值复制到新列中,python-3.x,pandas,Python 3.x,Pandas,我试图使用apply函数将行子集中的所有值复制到一个新列中,但它似乎只是复制整个数据帧范围。结果,我收到了数据帧的子集,尽管我希望df.loc[索引,'consolidated_Commentation']在所有_Commentation_列中包含的列中包含文本的连接版本 我的代码是: for index, row in df[all_commentary_columns].iterrows(): if pd.isna(row).prod(): df.loc[index, 'new_c

我试图使用apply函数将行子集中的所有值复制到一个新列中,但它似乎只是复制整个数据帧范围。结果,我收到了数据帧的子集,尽管我希望df.loc[索引,'consolidated_Commentation']在所有_Commentation_列中包含的列中包含文本的连接版本

我的代码是:

 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)

我从我在评论中提到的答案中得出:

df.loc[index, 'consolidated_commentary'] = df.loc[index, all_commentary_columns].astype(str).apply(lambda x: ' '.join(' '.join(x).split()),axis=1)

.loc[index]
中的
索引是什么?请提供您的示例数据和预期输出。@QuangHoang已更新!请参阅以下答案:谢谢,尽管它似乎没有按预期连接行值:/