Home > Back-end >  Split CSV into multiple files based on column value
Split CSV into multiple files based on column value

Time:01-25

I have a poorly-structured CSV file named file.csv, and I want to split it up into multiple CSV using Python.

|A|B|C|
|Continent||1|
|Family|44950|file1|
|Species|44950|12|
|Habitat||4|
|Species|44950|22|
|Condition|Tue Jan 24 00:00:00 UTC 2023|4|
|Family|Fish|file2|
|Species|Bass|8|
|Species|Trout|2|
|Habitat|River|3|

The new files need to be separated based on everything between the Family rows, so for example:

file1.csv

|Family|44950|file1|
|Species|44950|12|
|Habitat||4|
|Species|44950|22|
|Condition|Tue Jan 24 00:00:00 UTC 2023|4|

file2.csv

|Family|Fish|file2|
|Species|Bass|8|
|Species|Trout|2|
|Habitat|River|3|

What's the best way of achieving this when the number of rows between appearances of Species is not consistent?

CodePudding user response:

The best way to split a CSV file into multiple CSV files based on certain conditions is to use the pandas library in Python. You can use the following steps to achieve this:

  1. Read the CSV file: Use the pandas.read_csv() function to read the file.csv file into a DataFrame.
  2. Identify the Family rows: Use the DataFrame query() method and a boolean expression to identify the rows in the DataFrame where the value of column 'A' is 'Family'.
  3. Split the DataFrame: Use the groupby() method and the boolean expression from step 2 to group the DataFrame by the Family rows, this will split the DataFrame into multiple DataFrames, one for each group of rows between two consecutive 'Family' rows.
  4. Write the DataFrames to CSV: Use the to_csv() method to write each of the new DataFrames to a separate CSV file. Be sure to use a unique file name for each CSV file.
  5. Iterate through the dataframe and write every time a 'Family' is found.

Here's an example of how the code would look like:

import pandas as pd

df = pd.read_csv("file.csv")

# Identify the Family rows
family_rows = df['A'] == 'Family'

# Split the DataFrame
for family, group in df[family_rows].groupby(family_rows.cumsum()):
    group.to_csv(f"file{family}.csv", index=False)

This code will read the file.csv file, group the rows by the family rows, write each group to a separate file and will use the variable family to name the new files.

This is a basic example, you could enhance this script to check for missing values, to handle different types of files or to handle different types of missing values.

It's important to keep in mind that the CSV file that you provided doesn't have a consistent structure, so the script should be adjusted accordingly and test it multiple times before running it on the original file, specially if you have a large dataset.

CodePudding user response:

import pandas as pd
pd.read_csv('file.csv',delimiter='|')
groups = df.groupby('Family')
for name, group in groups:
    group.to_csv(name   '.csv', index=False)

CodePudding user response:

Here is a pure python working method:

# Read file
with open('file.csv', 'r') as file:
    text = file.read()

# Split using |Family|
splitted_text = text.split("|Family|")

# Remove unwanted content before first |Family|
splitted_text = splitted_text[1:]

# Add |Family| back to each part
splitted_text = ['|Family|'   item for item in splitted_text]

# Write files
for i, content in enumerate(splitted_text ):
    with open('file{}.csv'.format(i), 'w') as file:
        file.write(content)
  • Related