从4个指定的列中仅获取两个值,并将有效值合并到2个列中

时间:2018-11-07 06:22:58

标签: python pandas dataframe pandas-groupby

df:

index      a          b          c           d
-
0          1          2          NaN         NaN
1          2          NaN        3           NaN
2          5          NaN        6           NaN
3          1          NaN        NaN         5

df期望:

index      one        two
-
0          1          2         
1          2          3     
2          5          6        
3          1          5

上面的输出示例是不言自明的。基本上,我只需要将[Na,N]列中的两个值从[a,b,c,d]列移到另外两个两列[“ one”,“ two”]

2 个答案:

答案 0 :(得分:4)

使用回填缺失值并选择前两列:

df = df.bfill(axis=1).iloc[:, :2].astype(int)
df.columns = ["one", "two"]
print (df)
       one  two
index          
0        1    2
1        2    3
2        5    6
3        1    5

答案 1 :(得分:1)

combine_first + drop

df['two']=df.pop('b').combine_first(df.pop('c')).combine_first(df.pop('d'))
df=df.drop(['b','c','d'],1)
df.columns=['index','one','two']

fillna

df['two']=df.pop('b').fillna(df.pop('c')).fillna(df.pop('d'))
df=df.drop(['b','c','d'],1)
df.columns=['index','one','two']

两种情况:

print(df)

是:

   index  one  two
0      0    1  2.0
1      1    2  3.0
2      2    5  6.0
3      3    1  5.0

如果要输出类似@jezrael的输出,请添加:(两种情况都可以)

df=df.set_index('index')

然后:

print(df)

是:

       one  two
index          
0        1  2.0
1        2  3.0
2        5  6.0
3        1  5.0