[英]Getting list of all columns corresponding to maximum value in each row in a Dataframe
[英]For each row in a dataframe, return a list of columns that are NaN. But not all columns, only those in a given list
查看构建一些错误日志并尝试在特定列中捕获 null 值。
Essentially, I want to go from a dataframe and list of columns, to then output a dataframe with a column containing which of those columns from the list are null for each row. 请注意,我也会对负值等执行此操作。
例子:
columns_list = ['A','B','D']
日期 | 一个 | 乙 | C | D |
---|---|---|---|---|
2022-01-01 | 1 | 22 | 1231 | -121 |
2022-01-02 | 11 | 钠 | 钠 | 钠 |
2022-01-03 | 钠 | 52 | 12 | 0 |
2022-01-04 | 11 | 27 | 钠 | 3434 |
以下代码将给出以下 output 但我希望能够使用 columns_list 来避免在 X 中返回列 C:
df['X']= df.apply(lambda x: ','.join(x[x.isnull()].index), axis=1)
日期 | 一个 | 乙 | C | D | X |
---|---|---|---|---|---|
2022-01-02 | 11 | 钠 | 钠 | 钠 | B,C,D |
2022-01-03 | 钠 | 52 | 12 | 0 | 一个 |
2022-01-04 | 11 | 27 | 钠 | 3434 | C |
提前谢谢大家!
只需对您的列进行子集化:
df['X']= df[columns_list].apply(lambda x: ','.join(x[x.isnull()].index), axis=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.