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.py527 chars18 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.
gistlibby LogSnag