python - Does pandas reindex `method` kwag not work with columns? -


i trying 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()) 

and this:

          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, expect 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 

i can achieve goal battery of .fillna , slicing, there more elegant? (note, using pandas 0.18.1)

starting 3 column data frame,

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 

to expand columns , forward fill along rows, can reindex columns firstly , ffill() axis = 1 specify fill direction 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 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -