'Change single column vector into multiple columns using Pandas
I have values in a single column and multiple rows in this form (as a Pandas dataframe)
import pandas as pd
#Create data
df = pd.DataFrame(np.linspace(1,20,20))
print(df)
1
2
:
19
20
How can I take these values and split them into multiple rows do that each column has some multiple of the original matrix say 5, e.g.
1 6 11 16
2 7 12 17
3 8 13 18
4 9 14 19
5 10 15 20
Solution 1:[1]
Say I want to take a 1D array or a Series of length 20 and I want to split it into 4 equal sized columns of size 5. This is how I would do it:
pd.DataFrame(np.linspace(1,20,20).reshape(-1, 5).T)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user4933 |
