[英]Pandas move column values to first row within same column if Nan
How do I go from this df
, 我如何从这个
df
转到
c0 c1 c2 c3
0 5.99 NaN NaN NaN
1 NaN 12 NaN NaN
2 NaN NaN 44 NaN
3 NaN NaN NaN 49
to this df
, 为此
df
c0 c1 c2 c3
0 5.99 12 44 49
That is, 那是,
Per column, there's just 1 non-NaN value. 每列只有1个非NaN值。
Use bfill
with axis=0
and downcast='infer'
+ dropna
: 使用
bfill
与axis=0
和downcast='infer'
+ dropna
:
df = df.bfill(axis=0, downcast='infer').dropna()
print(df)
c0 c1 c2 c3
0 5.99 12 44 49
Without downcast='infer'
you get all float data types: 如果不使用
downcast='infer'
您将获得所有float数据类型:
print(df.bfill(0).dropna())
c0 c1 c2 c3
0 5.99 12.0 44.0 49.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.