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

To read a CSV file in a folder and split the second column into multiple 25-row columns in Python, you can use the pandas library. Here's an example of how you can achieve this:

main.py
import pandas as pd

# Folder path where CSV files are stored
folder_path = 'path_to_folder/'

# List to store the data from each file
data = []

# Read all CSV files in the folder
for file in os.listdir(folder_path):
    if file.endswith('.csv'):
        file_path = os.path.join(folder_path, file)
        df = pd.read_csv(file_path)
        data.append(df)

# Concatenate all dataframes along the row axis
merged_df = pd.concat(data)

# Split the second column into multiple 25-row columns
split_columns = [merged_df.iloc[i:i+25, 1].reset_index(drop=True) for i in range(0, len(merged_df), 25)]
result = pd.concat(split_columns, axis=1)

# Print the resulting dataframe
print(result)
687 chars
25 lines

This code will iterate over all the CSV files in the specified folder, read them using pd.read_csv(), and store the datasets in a list called data. Then, it concatenates all the dataframes in data into a single dataframe called merged_df.

Next, the second column of merged_df is split into multiple columns, with each column containing 25 rows. This is achieved by using a list comprehension and the iloc function from pandas. Finally, the resulting split columns are concatenated along the column axis using pd.concat().

Note: Don't forget to import the necessary libraries (pandas and os) at the beginning of your code.

related categories

gistlibby LogSnag