我正在尝试这个简单的操作:
df = pd.DataFrame(np.random.rand(5,3))
df.columns = df.columns * 3
print(df.reindex(columns=np.arange(9), method='ffill').head())
我明白了:
0 1 2 3 4 5 6 7 8
0 0.593936 NaN NaN 0.805081 NaN NaN 0.930780 NaN NaN
1 0.019330 NaN NaN 0.095645 NaN NaN 0.667744 NaN NaN
2 0.826164 NaN NaN 0.295915 NaN NaN 0.259967 NaN NaN
3 0.495695 NaN NaN 0.403194 NaN NaN 0.122684 NaN NaN
4 0.365294 NaN NaN 0.648194 NaN NaN 0.621820 NaN NaN
但是,当然,我希望看到这个:
0 1 2 3 4 5 6 7 8
0 0.593936 0.593936 0.593936 0.805081 0.805081 0.805081 0.930780 0.930780 0.930780
1 0.019330 0.019330 0.019330 0.095645 0.095645 0.095645 0.667744 0.667744 0.667744
2 0.826164 0.826164 0.826164 0.295915 0.295915 0.295915 0.259967 0.259967 0.259967
3 0.495695 0.495695 0.495695 0.403194 0.403194 0.403194 0.122684 0.122684 0.122684
4 0.365294 0.365294 0.365294 0.648194 0.648194 0.648194 0.621820 0.621820 0.621820
我可以通过.fillna
和切片来实现我的目标,但有更优雅的东西吗? (注意,我使用的是pandas 0.18.1)
答案 0 :(得分:0)
从这三列数据框开始,
df
# 0 3 6
#0 0.563483 0.404885 0.336098
#1 0.097173 0.141343 0.564055
#2 0.715131 0.185852 0.361714
#3 0.841248 0.028190 0.754315
#4 0.475066 0.592953 0.839101
要展开列并沿行向前填充,您可以先reindex
列,然后ffill()
axis = 1
以指定沿行的填充方向:
df.reindex(columns=np.arange(15)).ffill(axis = 1)
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
#0 0.563483 0.563483 0.563483 0.404885 0.404885 0.404885 0.336098 0.336098 0.336098 0.336098 0.336098 0.336098 0.336098 0.336098 0.336098
#1 0.097173 0.097173 0.097173 0.141343 0.141343 0.141343 0.564055 0.564055 0.564055 0.564055 0.564055 0.564055 0.564055 0.564055 0.564055
#2 0.715131 0.715131 0.715131 0.185852 0.185852 0.185852 0.361714 0.361714 0.361714 0.361714 0.361714 0.361714 0.361714 0.361714 0.361714
#3 0.841248 0.841248 0.841248 0.028190 0.028190 0.028190 0.754315 0.754315 0.754315 0.754315 0.754315 0.754315 0.754315 0.754315 0.754315
#4 0.475066 0.475066 0.475066 0.592953 0.592953 0.592953 0.839101 0.839101 0.839101 0.839101 0.839101 0.839101 0.839101 0.839101 0.839101