Python 如何将满足一定条件的列的标题返回到表中的新列
对于下面的dataframe,如何将大于1的列的标题名返回到名为“备注”的新列 希望输出如下Python 如何将满足一定条件的列的标题返回到表中的新列,python,pandas,Python,Pandas,对于下面的dataframe,如何将大于1的列的标题名返回到名为“备注”的新列 希望输出如下 A B C Remark 0 1 1 2 C 1 2 2 2 A,B,C 2 1 3 1 B 3 4 5 2 A,B,C 提前感谢您按列名称使用带分隔符的布尔数据框,最后删除,: df['Remark'] = df.gt(1).dot(df.columns + ',').str[:-1] print (df)
A B C Remark
0 1 1 2 C
1 2 2 2 A,B,C
2 1 3 1 B
3 4 5 2 A,B,C
提前感谢您按列名称使用带分隔符的布尔数据框
,最后删除,
:
df['Remark'] = df.gt(1).dot(df.columns + ',').str[:-1]
print (df)
A B C Remark
0 1 1 2 C
1 2 2 2 A,B,C
2 1 3 1 B
3 4 5 2 A,B,C
详细信息:
print (df.gt(1))
A B C
0 False False True
1 True True True
2 False True False
3 True True True
print (df.gt(1).dot(df.columns + ','))
0 C,
1 A,B,C,
2 B,
3 A,B,C,
dtype: object
,非常感谢。
print (df.gt(1))
A B C
0 False False True
1 True True True
2 False True False
3 True True True
print (df.gt(1).dot(df.columns + ','))
0 C,
1 A,B,C,
2 B,
3 A,B,C,
dtype: object