python - 每行获取第一个非空值

标签 python pandas dataframe

我有一个示例数据框显示如下。 对于每一行,我想先检查 c1,如果它不为空,然后检查 c2。通过这种方式,找到第一个非空列并将该值存储到列结果中。

ID  c1  c2  c3  c4  result
1   a   b           a
2       cc  dd      cc
3           ee  ff  ee
4               gg  gg

目前我是用这种方式。但我想知道是否有更好的方法。(列名没有任何模式,这只是示例)

df["result"] = np.where(df["c1"].notnull(), df["c1"], None)
df["result"] = np.where(df["result"].notnull(), df["result"], df["c2"])
df["result"] = np.where(df["result"].notnull(), df["result"], df["c3"])
df["result"] = np.where(df["result"].notnull(), df["result"], df["c4"])
df["result"] = np.where(df["result"].notnull(), df["result"], "unknown)

当有很多列时,这种方法看起来不太好。

最佳答案

先使用回填NaN然后通过iloc选择第一列:

df['result'] = df[['c1','c2','c3','c4']].bfill(axis=1).iloc[:, 0].fillna('unknown')

或者:

df['result'] = df.iloc[:, 1:].bfill(axis=1).iloc[:, 0].fillna('unknown')

print (df)
   ID   c1   c2  c3   c4 result
0   1    a    b   a  NaN      a
1   2  NaN   cc  dd   cc     cc
2   3  NaN   ee  ff   ee     ee
3   4  NaN  NaN  gg   gg     gg

性能:

df = pd.concat([df] * 1000, ignore_index=True)


In [220]: %timeit df['result'] = df[['c1','c2','c3','c4']].bfill(axis=1).iloc[:, 0].fillna('unknown')
100 loops, best of 3: 2.78 ms per loop

In [221]: %timeit df['result'] = df.iloc[:, 1:].bfill(axis=1).iloc[:, 0].fillna('unknown')
100 loops, best of 3: 2.7 ms per loop

#jpp solution
In [222]: %%timeit
     ...: cols = df.iloc[:, 1:].T.apply(pd.Series.first_valid_index)
     ...: 
     ...: df['result'] = [df.loc[i, cols[i]] for i in range(len(df.index))]
     ...: 
1 loop, best of 3: 180 ms per loop

#cᴏʟᴅsᴘᴇᴇᴅ'  s solution
In [223]: %timeit df['result'] = df.stack().groupby(level=0).first()
1 loop, best of 3: 606 ms per loop

关于python - 每行获取第一个非空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50004529/

相关文章:

python - 全局名称 'debug' 未定义

python - PyGtk3,切换按钮, "toggled-or-untoggled"事件

python - 列出 Pandas 数据框 - Python

python - 将值设置为 pandas 数据框的整列

python - 使用隐马尔可夫模型进行情感分析

python - 如何获得整个数据框而不是列的平均值?

r - Python Pandas 中是否有像 index_col 这样的 DataFrames 的 R 方法?

python - Pandas 将零替换为最接近的平均非零值

python - Pandas 滚动窗口返回一个数组

python - SQLAlchemy 中的多对多查询