CiaranWelsh
CiaranWelsh

Reputation: 7681

How to restructure a pandas dataframe even in the presence of missing data

If I had a pandas DataFrame which looks like this:

    df=pandas.DataFrame(range(8))



   0
0  0
1  1
2  2
3  3
4  4
5  5
6  6
7  7

What would be the best way to restructure this frame into two columns of five rows, regardless of the fact I only have 8 numbers?

so the output would be :

   0  1
0  0  5
1  1  6 
2  2  7
3  3  NaN
4  4  NaN
5  5  NaN

Upvotes: 3

Views: 10252

Answers (2)

Steven G
Steven G

Reputation: 17152

just so you know there is a numpy function to resize your arrays. it does not fill them with Na's tho, it could repeat your values or it could add 0 in missing spots.

adding 0s:

import numpy as np
df=pandas.DataFrame(range(8))
arr = df.values.copy()
arr.resize(2, 5)
pd.DataFrame(arr.T)
Out[53]: 
   0  1
0  0  5
1  1  6
2  2  7
3  3  0
4  4  0

repeating values for missing spots:

import numpy as np
df=pandas.DataFrame(range(8))
pd.DataFrame(np.resize(df.values, (2, 5)).T)
Out[61]: 
   0  1
0  0  5
1  1  6
2  2  7
3  3  0
4  4  1

Upvotes: 2

Julien Marrec
Julien Marrec

Reputation: 11905

Try this:

pd.DataFrame([df[0].values[:6], df[0].values[6:]]).T

     0    1
0  0.0  5.0
1  1.0  6.0
2  2.0  7.0
3  3.0  NaN
4  4.0  NaN

And if you really want to repeat the 5 twice:

pd.DataFrame([df[0].values[:6], df[0].values[5:]]).T


     0    1
0  0.0  5.0
1  1.0  6.0
2  2.0  7.0
3  3.0  NaN
4  4.0  NaN
5  5.0  NaN

Upvotes: 2

Related Questions