简体   繁体   English

仅保留每行中的第一个非空值(并用 NaN 替换其他值)

[英]Keep only the 1st non-null value in each row (and replace others with NaN)

I have the following dataframe:我有以下 dataframe:

     a   b
0  3.0  10.0
1  2.0   9.0
2  NaN   8.0

For each row, I need to drop (and replace with NaN) all values, excluding the first non-null one.对于每一行,我需要删除(并用 NaN 替换)所有值,不包括第一个非空值。 This is the expected output:这是预期的 output:

     a   b
0  3.0   NaN
1  2.0   NaN
2  NaN   8.0

I know that using the justify function I can identify the first n non-null values, but I need to keep the same structure of the original dataframe.我知道使用justify function我可以识别前 n 个非空值,但我需要保持与原始 dataframe 相同的结构。

try this:尝试这个:

f = df.copy()
f[:] = f.columns
fv_idx = df.apply(pd.Series.first_valid_index, axis=1).values[:, None]
res = df.where(f == fv_idx)
print(res)
>>>
    a   b
0   3.0 NaN
1   2.0 NaN
2   NaN 8.0

One way to go, would be: go 的一种方法是:

import pandas as pd

data = {'a': {0: 3.0, 1: 2.0, 2: None}, 'b': {0: 10.0, 1: 9.0, 2: 8.0}}

df = pd.DataFrame(data)

def keep_first_valid(x):
    first_valid = x.first_valid_index()
    return x.mask(x.index!=first_valid)

df = df.apply(lambda x: keep_first_valid(x), axis=1)
df

     a    b
0  3.0  NaN
1  2.0  NaN
2  NaN  8.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM