read individual csv files in a folder and split the second column into multiple 25 row columns in python

You can use the pandas library in Python to read individual CSV files in a folder and split the second column into multiple 25-row columns. Here's an example code snippet to achieve this:

main.py
import pandas as pd
import os

# Directory path for CSV files
folder_path = 'path_to_folder'

# Empty dataframe to store the result
combined_df = pd.DataFrame()

# Loop through each file in the folder
for file_name in os.listdir(folder_path):
    if file_name.endswith('.csv'):
        # Read the CSV file
        file_path = os.path.join(folder_path, file_name)
        df = pd.read_csv(file_path)
        
        # Split the second column into multiple columns with 25 rows each
        split_cols = [df.iloc[i:i+25, 1] for i in range(0, len(df), 25)]
        split_df = pd.concat(split_cols, axis=1)
        
        # Concatenate the split dataframe to the combined dataframe
        combined_df = pd.concat([combined_df, split_df], axis=1)

# Print the combined dataframe
print(combined_df)
797 chars
26 lines

In this code snippet, we first set the folder_path variable to the directory path where the CSV files are located.

Then, we initialize an empty dataframe called combined_df to store the final result.

Next, we loop through each file in the folder using the os.listdir function. If the file has the extension .csv, we read it using pd.read_csv and store the data in the df dataframe.

We then split the second column of the df dataframe into multiple columns, each with 25 rows, using a list comprehension and the iloc method of pandas dataframe. Each split column is stored in the split_cols list.

Finally, we use pd.concat to concatenate the split columns horizontally (axis=1) and then concatenate the result to the combined_df dataframe.

At the end of the loop, the combined_df dataframe will contain the desired result, with the second column of each CSV file split into multiple 25-row columns.

Note: Don't forget to replace 'path_to_folder' with the actual path to your folder containing the CSV files.

related categories

gistlibby LogSnag