I have a data frame with 1 column and 2880 values.
Is there any easy way to convert it in a dataframe with 96 rows and 30 columns?
thanks in advance!
for example if I had 10 elements in one column to make a new data frame with 5 elements and 2 columns
CodePudding user response:
You can try this.
I have shown a sample code, which has a dataframe with 1 column and 12 values. We want to convert it into a dataframe of the shape: 3x4 (3 rows and 4 columns).
import pandas as pd
data = {'id': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]}
df = pd.DataFrame(data)
print(df)
The data before changing the shape.
new_data = pd.DataFrame()
for i in range(4): # in your use case you can write 30 instead of 4
# here, you can write 96 instead of 3
# so, it will look like this df.iloc[(96 * i): ((i 1) * 96)]
new_data.insert(i, f'col-{i}', df.iloc[(3 * i):((i 1) *3)])
print(new_data)
Data after changing the shape
I hope this helps.