split column 3 into multiple columns of 25 rows in python

To split a single column into multiple columns of 25 rows each in Python using the pandas library, you can use the reshape function.

Assuming you have a DataFrame named df with 3 columns and you want to split the third column into multiple columns of 25 rows, you can follow these steps:

main.py
import pandas as pd

# Create a sample DataFrame
df = pd.DataFrame({'Col1': range(100),
                   'Col2': range(100, 200),
                   'Col3': range(200, 300)})

# Split the third column into multiple columns of 25 rows each
split_columns = 25
new_columns = [f'Col3_{i + 1}' for i in range(len(df) // split_columns)]
df[new_columns] = pd.DataFrame(df['Col3'].to_numpy().reshape(-1, split_columns))

# Drop the original third column
df.drop('Col3', axis=1, inplace=True)

# Print the updated DataFrame
print(df)
527 chars
18 lines

In this example, we start by creating a sample DataFrame with 3 columns. We then set the split_columns variable to 25, which represents the number of rows each new column should have. Next, we create a list of new column names new_columns, each with a numerical suffix.

Using the reshape function, we reshape the values in the 'Col3' column into a 2-dimensional numpy array with dimensions (-1, split_columns). The -1 in the reshape function tells numpy to automatically infer the number of rows based on the number of columns and the length of the original column.

We then use the pd.DataFrame() function to convert the reshaped numpy array into a DataFrame and assign it to the new columns in the original DataFrame.

Finally, we drop the original 'Col3' column from the DataFrame using the drop() function with axis=1 (column-wise) and inplace=True to modify the DataFrame in-place.

The resulting DataFrame will have the third column split into multiple columns of 25 rows each.

related categories

gistlibby LogSnag