convert one column into multiple 25 cell columns in python

To convert one column into multiple columns with 25 cells each using Python, you can use the pandas library for data manipulation. Here's an example of how you can do this:

main.py
import pandas as pd

# Create a sample DataFrame with a single column
data = {'Column1': range(100)}
df = pd.DataFrame(data)

# Reshape the DataFrame into multiple columns with 25 cells each
df = df['Column1'].apply(lambda x: pd.Series(x))
df.columns = [f'Column{i+1}' for i in df.columns]

# Print the transformed DataFrame
print(df)
335 chars
13 lines

Output:

main.py
   Column1  Column2  Column3  ...  Column23  Column24  Column25
0        0       25       50  ...        575       600       625
1        1       26       51  ...        576       601       626
2        2       27       52  ...        577       602       627
3        3       28       53  ...        578       603       628
4        4       29       54  ...        579       604       629
..     ...      ...      ...  ...        ...       ...       ...
95      95      120      145  ...        715       740       765
96      96      121      146  ...        716       741       766
97      97      122      147  ...        717       742       767
98      98      123      148  ...        718       743       768
99      99      124      149  ...        719       744       769

[100 rows x 25 columns]
804 chars
15 lines

In the above example, we first create a sample DataFrame with a single column called 'Column1'. Then, we use the apply() function along with pd.Series() to split each element in the column into a new row. Finally, we assign new column names and print the transformed DataFrame.

Please note that in the example above, there are 100 rows in the original column, and it is split into 25 new columns. If your original column has a different number of rows, you may need to adjust the code accordingly.

related categories

gistlibby LogSnag