在python中转换数据框 [英] Transforming data frame in python
本文介绍了在python中转换数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下数据集:
ID A B C
1 aa - -
2 - bb -
3 - - cc
4 aaa - -
应该转换为以下数据框:
that should be transformed to the following data frame:
ID A
1 aa
2 bb
3 cc
4 aa
所以基本上移动行以适合第一列
So essentially shifting rows so that it fits to the first column
推荐答案
您可以在 replace
符号 '-' 后使用 bfill
和沿列的轴:
you can use bfill
with axis along columns after replace
the symbol '-' by nan:
df_ = df.replace('-', np.nan).bfill(1)[['ID', 'A']]
print(df_)
ID A
0 1 aa
1 2 bb
2 3 cc
3 4 aaa
这篇关于在python中转换数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文