按行 pandas 的非空值的第一列名称 [英] First column name with non null value by row pandas
本文介绍了按行 pandas 的非空值的第一列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道第一年各种项目的收入.
I want know the first year with incoming revenue for various projects.
给出以下数据框:
ID Y1 Y2 Y3
0 NaN 8 4
1 NaN NaN 1
2 NaN NaN NaN
3 5 3 NaN
我想按行返回第一列的名称,该列的值必须为非null.
I would like to return the name of the first column with a non-null value by row.
在这种情况下,我想返回:
In this case, I would want to return:
['Y2','Y3',NaN,'Y1']
我的目标是将此列添加为原始数据框.
My goal is to add this as a column to the original dataframe.
以下代码通常可以正常工作,但是确实很笨拙.
The following code mostly works, but is really clunky.
import pandas as pd
import numpy as np
df = pd.DataFrame({'Y1':[np.nan, np.nan, np.nan, 5],'Y2':[8, np.nan, np.nan, 3], 'Y3':[4, 1, np.nan, np.nan]})
df['first'] = np.nan
for ID in df.index:
row = df.loc[ID,]
for i in range(0,len(row)):
if (~pd.isnull(row[i])):
df.loc[ID,'first'] = row.index[i]
break
返回:
Y1 Y2 Y3 first
0 NaN 8 4 Y2
1 NaN NaN 1 Y3
2 NaN NaN NaN first
3 5 3 NaN Y1
有人知道更优雅的解决方案吗?
Does anyone know a more elegant solution?
推荐答案
您可以使用轴= 1的lambda表达式将first_valid_index
应用于数据框中的每一行.
You can apply first_valid_index
to each row in the dataframe using a lambda expression with axis=1 to specify rows.
>>> df.apply(lambda row: row.first_valid_index(), axis=1)
ID
0 Y2
1 Y3
2 None
3 Y1
dtype: object
要将其应用于您的数据框:
To apply it to your dataframe:
df = df.assign(first = df.apply(lambda row: row.first_valid_index(), axis=1))
>>> df
Y1 Y2 Y3 first
ID
0 NaN 8 4 Y2
1 NaN NaN 1 Y3
2 NaN NaN NaN None
3 5 3 NaN Y1
这篇关于按行 pandas 的非空值的第一列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文