I have a dataframe with x rows and would like to split it into x rows into column in a new dataframe.
I have found this example (30 rows in the x dataframe), where it works if I split it to something where row x col = 30 (below 10 rows en each columns => 3 col)
k = pd.concat([pd.Series(j, name='y' str(i), index=range(0,10)) for i,j in enumerate(np.array_split(x['TEST'].values, 3))], axis=1)
But if I would like e.g. 8 rows in columns which mean 4 columns, the last column would not be of the size 8 and I get this error
ValueError: Length of values (7) does not match length of index (8)
So how can I split a column into x chunks and then automatic get y columns in new dataframe?
CodePudding user response:
Instead of specifying to number of chunks you can specify the indices where to split:
x = pd.DataFrame({'TEST': range(30)})
n = 8
pd.concat([pd.Series(j, name='y' str(i)) for i,j in enumerate(np.split(x['TEST'].to_numpy(), range(n, len(x['TEST']), n)))], axis=1)
Result:
y0 y1 y2 y3
0 0 8 16 24.0
1 1 9 17 25.0
2 2 10 18 26.0
3 3 11 19 27.0
4 4 12 20 28.0
5 5 13 21 29.0
6 6 14 22 NaN
7 7 15 23 NaN