Open In App

How to create multiple CSV files from existing CSV file using Pandas ?

Last Updated : 22 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn how to create multiple CSV files from existing CSV file using Pandas. When we enter our code into production, we will need to deal with editing our data files. Due to the large size of the data file, we will encounter more problems, so we divided this file into some small files based on some criteria like splitting into rows, columns, specific values of columns, etc. 

First, let’s create a simple CSV file and use it for all examples below in the article. Create dataset using dataframe method of pandas and then save it to “Customers.csv” file or we can load existing dataset with the Pandas read_csv() function.

Python3




import pandas as pd
 
# initialise data dictionary.
data_dict = {'CustomerID': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
              
             'Gender': ["Male", "Female", "Female", "Male",
                        "Male", "Female", "Male", "Male",
                        "Female", "Male"],
              
             'Age': [20, 21, 19, 18, 25, 26, 32, 41, 20, 19],
              
             'Annual Income(k$)': [10, 20, 30, 10, 25, 60, 70,
                                   15, 21, 22],
              
             'Spending Score': [30, 50, 48, 84, 90, 65, 32, 46,
                                12, 56]}
 
# Create DataFrame
data = pd.DataFrame(data_dict)
 
# Write to CSV file
data.to_csv("Customers.csv")
 
# Print the output.
print(data)


Output:

Creating multiple  CSV files from the existing CSV file

To do our work, we will discuss different methods that are as follows:

Method 1: Splitting based on rows

In this method, we will split one CSV file into multiple CSVs based on rows.

Python3




import pandas as pd
 
# read DataFrame
data = pd.read_csv("Customers.csv")
 
# no of csv files with row size
k = 2
size = 5
 
for i in range(k):
    df = data[size*i:size*(i+1)]
    df.to_csv(f'Customers_{i+1}.csv', index=False)
 
df_1 = pd.read_csv("Customers_1.csv")
print(df_1)
 
df_2 = pd.read_csv("Customers_2.csv")
print(df_2)


Output:

Method 2: Splitting based on columns

Example 1:

Using groupby() method of Pandas we can create multiple CSV files. To create a file we can use the to_csv() method of  Pandas. Here created two files based on “male” and “female” values of Gender columns. 

Python3




import pandas as pd
 
# read DataFrame
data = pd.read_csv("Customers.csv")
 
for (gender), group in data.groupby(['Gender']):
     group.to_csv(f'{gender}.csv', index=False)
 
print(pd.read_csv("Male.csv"))
print(pd.read_csv("Female.csv"))


Output: 

Male.csv

Female.csv

Example 2:

We can group more than two columns and can create multiple files on the basis of a combination of unique values from both Columns value. Take Gender and Annual Income columns. 

Python3




import pandas as pd
 
# read DataFrame
data = pd.read_csv("Customers.csv")
 
for (Gender, Income), group in data.groupby(['Gender', 'Annual Income(k$)']):
    group.to_csv(f'{Gender} {Income}.csv', index=False)
    print(pd.read_csv(f'{Gender} {Income}.csv'))


Output:

All Nine CSV files

Example 3:

We will filter the columns based on the specific column name Gender to its values (Male and Female). Then convert that to CSV file using to_csv in pandas. 

Python3




import pandas as pd
 
# read DataFrame
data = pd.read_csv("Customers.csv")
 
male = data[data['Gender'] == 'Male']
female = data[data['Gender'] == 'Female']
 
male.to_csv('Gender_male.csv', index=False)
female.to_csv('Gender_female.csv', index=False)
 
print(pd.read_csv("Gender_male.csv"))
print(pd.read_csv("Gender_female.csv"))


Output:

Method 3: Splitting based both on Rows and Columns 

Using groupby() method of Pandas we can create multiple CSV files row-wise. To create a file we can use the to_csv() method of Pandas. Here created two files based on row values “male” and “female” values of specific Gender column for Spending Score.

Python3




for (gender), group in data['Spending Score'].groupby(data['Gender']):
    group.to_csv(f'{gender}Score.csv', index=False)
     
print(pd.read_csv("MaleScore.csv"))
print(pd.read_csv("FemaleScore.csv"))


Output:



Previous Article
Next Article

Similar Reads

How to Append Pandas DataFrame to Existing CSV File?
In this discussion, we'll explore the process of appending a Pandas DataFrame to an existing CSV file using Python. Add Pandas DataFrame to an Existing CSV File. To achieve this, we can utilize the to_csv() function in Pandas with the 'a' parameter to write the DataFrame to the CSV file in append mode. Pandas DataFrame to_csv() Syntax Syntax : df.t
3 min read
How to Merge multiple CSV Files into a single Pandas dataframe ?
While working with CSV files during data analysis, we often have to deal with large datasets. Sometimes, it might be possible that a single CSV file doesn't consist of all the data that you need. In such cases, there's a need to merge these files into a single data frame. Luckily, the Pandas library provides us with various methods such as merge, c
3 min read
How to create a duplicate file of an existing file using Python?
In this article, we will discuss how to create a duplicate of the existing file in Python. Below are the source and destination folders, before creating the duplicate file in the destination folder. After a duplicate file has been created in the destination folder, it looks like the image below. For automating of copying and removal of files in Pyt
5 min read
Copying Csv Data Into Csv Files Using Python
CSV files, the stalwarts of information exchange, can be effortlessly harnessed to extract specific data or merge insights from multiple files. In this article, we unveil five robust approaches, transforming you into a virtuoso of CSV data migration in Python. Empower your data-wrangling endeavors, mastering the art of copying and organizing inform
4 min read
How to append a new row to an existing csv file?
For writing a CSV file, the CSV module provides two different classes writer and Dictwriter. Here we will discuss 2 ways to perform this task effectively. The First will be 'append a list as a new row to the existing CSV file' and second way is 'Append a dictionary as a new row to the existing CSV file.' First, let's have a look at our existing CSV
3 min read
Add a Column to Existing CSV File in Python
Working with CSV files is a common task in data manipulation and analysis, and Python provides versatile tools to streamline this process. Here, we have an existing CSV file and our task is to add a new column to the existing CSV file in Python. In this article, we will see how we can add a column to a CSV file in Python. Add a New Column to Existi
3 min read
Concatenating CSV files using Pandas module
Using pandas we can perform various operations on CSV files such as appending, updating, concatenating, etc. In this article, we are going to concatenate two CSV files using pandas module. Suppose we have one .csv file named Employee.csv which contains some records and it is as below: There is another .csv file named Updated.csv which contains new
3 min read
How to merge two csv files by specific column using Pandas in Python?
In this article, we are going to discuss how to merge two CSV files there is a function in pandas library pandas.merge(). Merging means nothing but combining two datasets together into one based on common attributes or column. Syntax: pandas.merge() Parameters : data1, data2: Dataframes used for merging.how: {‘left’, ‘right’, ‘outer’, ‘inner’}, def
3 min read
Create a GUI to convert CSV file into excel file using Python
Prerequisites: Python GUI – tkinter, Read csv using pandas CSV file is a Comma Separated Value file that uses a comma to separate values. It is basically used for exchanging data between different applications. In this, individual rows are separated by a newline. Fields of data in each row are delimited with a comma. Modules Needed Pandas: Python i
3 min read
Convert multiple JSON files to CSV Python
In this article, we will learn how to convert multiple JSON files to CSV file in Python. Before that just recall some terms : JSON File: A JSON file may be a file that stores simple data structures and objects in JavaScript Object Notation (JSON) format, which may be a standard data interchange format. It is primarily used for transmitting data bet
8 min read
Practice Tags :