user3059024 user3059024 - 10 days ago 6
Python Question

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

Answer

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
Comments