pandas 重新索引`method` kwag不适用于列吗? [英] Does pandas reindex `method` kwag not work with columns?

查看:73
本文介绍了 pandas 重新索引`method` kwag不适用于列吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行以下简单操作:

I am trying this simple operation:

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

但是,当然,我希望看到这个:

But, of course, I expect to see this:

           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和切片来实现我的目标,但是还有什么更优雅的选择吗? (请注意,我使用的是熊猫0.18.1)

I can achieve my goal with a battery of .fillna and slicing, but is there something more elegant? (Note, I am using pandas 0.18.1)

推荐答案

从这三列数据框开始,

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指定沿行的填充方向:

To expand the columns and forward fill along rows, you can reindex the columns firstly and then ffill() with axis = 1 to specify the fill direction to be along rows:

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

这篇关于 pandas 重新索引`method` kwag不适用于列吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆