Copying Csv Data Into Csv Files Using Python
Last Updated :
28 Apr, 2025
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 information with finesse.
Copy Csv Data Into Csv Files in Python
Below are some of the ways by which we can copy data between CSV files in Python:
The CSV file used is StudentPerformance.CSV.Contents of the file are
gender,race/ethnicity,parental level of education,lunch,test preparation course,math score,reading score,writing score
female,group B,bachelor's degree,standard,none,72,72,
female,group C,some college,standard,completed,69,90,88
female,group B,master's degree,standard,none,90,95,93
male,group A,associate's degree,free/reduced,none,47,57,44
male,group C,some college,standard,none,76,78,75
female,group B,associate's degree,standard,none,71,83,78
female,group B,some college,standard,completed,88,95,92
male,group B,some college,free/reduced,none,40,43,39
Copy Csv Data Into Csv Files Using CSV Module
This fundamental approach leverages the built-in csv module, offering fine-grained control over data movement. You'll open both files, iterate through each row in the source file, and selectively write desired data to the target file.
This code iterates through each row in student_scores2.csv, extracts elements at specific indices (0 and 2), modifies the second element by adding 10, and writes the modified row to target.csv. This showcases basic data transfer and manipulation within the loop.
Python3
import csv
with open("StudentPerformance.csv", "r") as source, open("target.csv", "w") as target:
reader = csv.reader(source)
writer = csv.writer(target)
for row in reader:
if row[2].isdigit():
new_row = [row[0], str(int(row[2]) + 10)]
else:
new_row = row # Handle rows with non-numerical data at index 2
# Print the selected or manipulated row to the console
print(new_row)
# Write the row to the target CSV file
writer.writerow(new_row)
print('\ncsv Data copied to target csv files')
Output:
['gender', 'race/ethnicity', 'parental level of education', 'lunch', 'test preparation course', 'math score', 'reading score', 'writing score']
['female', 'group B', "bachelor's degree", 'standard', 'none', '72', '72', '']
['female', 'group C', 'some college', 'standard', 'completed', '69', '90', '88']
['female', 'group B', "master's degree", 'standard', 'none', '90', '95', '93']
['male', 'group A', "associate's degree", 'free/reduced', 'none', '47', '57', '44']
['male', 'group C', 'some college', 'standard', 'none', '76', '78', '75']
['female', 'group B', "associate's degree", 'standard', 'none', '71', '83', '78']
['female', 'group B', 'some college', 'standard', 'completed', '88', '95', '92']
['male', 'group B', 'some college', 'free/reduced', 'none', '40', '43', '39']
csv Data copied to target csv files
Python Copy Csv Data Into Csv Files Using Pandas
In this example, we are using Pandas to copy CSV data into CSV files. Here reads StudentPerformance.csv into a Pandas dataframe, filters rows based on a condition in a specific column, and writes the filtered data to target.csv without including the dataframe index.
Python3
import pandas as pd
# Read the dataset
url= "https://media.geeksforgeeks.org/wp-content/uploads/20240206150437/StudentPerformance.csv"
df = pd.read_csv(url)
# Define the threshold value for "Hours"
threshold = 90 # Replace with your desired threshold
filtered_data = df[df["reading score"] > threshold]
# Print the filtered DataFrame to the console
print(filtered_data)
# Write the filtered data to the target CSV file
filtered_data.to_csv("target.csv", index=False)
print('\n Filtered csv Data copied to target csv files')
Output:
gender race/ethnicity parental level of education lunch \
2 female group B master's degree standard
6 female group B some college standard
test preparation course math score reading score writing score
2 none 90 95 93.0
6 completed 88 95 92.0
Filtered csv Data copied to target csv files
Merging Multiple Files with glob and Pandas
Need to combine the insights from multiple CSV files? Enter the dynamic duo of glob and Pandas. glob finds files matching a pattern, while Pandas seamlessly merges them into one DataFrame for further analysis or writing to a single CSV.
Python3
import pandas as pd
import os
directory = 'Data'
all_files = os.listdir(directory)
# Get the full file paths
file_paths = [os.path.join(directory, file) for file in all_files if file.split('.')[-1]=='csv']
# Print the file paths
print('All csv files path:\n',file_paths)
combined_data = pd.concat([pd.read_csv(f) for f in file_paths])
# Reset the index
combined_data.reset_index(inplace=True, drop=True)
# Print the combined DataFrame to the console
print(combined_data)
# Save the combined data to the CSV file
combined_data.to_csv("master_data.csv", index=False)
Output:
All csv files path:
['Data/StudentPerformance.csv', 'Data/target.csv']
gender race/ethnicity parental level of education lunch \
0 female group B bachelor's degree standard
1 female group C some college standard
2 female group B master's degree standard
3 male group A associate's degree free/reduced
4 male group C some college standard
5 female group B associate's degree standard
6 female group B some college standard
7 male group B some college free/reduced
8 female group B master's degree standard
9 female group B some college standard
test preparation course math score reading score writing score
0 none 72 72 NaN
1 completed 69 90 88.0
2 none 90 95 93.0
3 none 47 57 44.0
4 none 76 78 75.0
5 none 71 83 78.0
6 completed 88 95 92.0
7 none 40 43 39.0
8 none 90 95 93.0
9 completed 88 95 92.0
master_data.csv
Similar Reads
Writing CSV files in Python
CSV (Comma Separated Values) is a simple file format used to store tabular data, such as spreadsheets or databases. Each line of the file represents a data record, with fields separated by commas. This format is popular due to its simplicity and wide support.Ways to Write CSV Files in PythonBelow ar
11 min read
Reading CSV files in Python
A CSV (Comma Separated Values) file is a form of plain text document that uses a particular format to organize tabular information. CSV file format is a bounded text document that uses a comma to distinguish the values. Every row in the document is a data log. Each log is composed of one or more fie
5 min read
Sorting a CSV object by dates in Python
CSV stands for comma-separated values. A CSV file can be opened in Google Sheets or Excel and will be formatted as a spreadsheet. However, a CSV file is actually a plain-text file. It can also be opened with a text editor program such as Atom. In this article, we are going to see how to sort a CSV o
2 min read
Convert Text File to CSV using Python Pandas
Converting Text File to CSV using Python Pandas refers to the process of transforming a plain text file (often with data separated by spaces, tabs, or other delimiters) into a structured CSV (Comma Separated Values) file using the Python Pandas library.In this article we will walk you through multip
2 min read
Convert PDF to CSV using Python
Python is a high-level, general-purpose, and very popular programming language. Python programming language (the latest Python 3) is being used in web development, Machine Learning applications, along with all cutting-edge technology in Software Industry. Python Programming Language is very well sui
2 min read
Visualize data from CSV file in Python
CSV stands for Comma-Separated Values, which means that the data in a CSV file is separated by commas, making it easy to store tabular data. The file extension for CSV files is .csv, and these files are commonly used with spreadsheet applications like Google Sheets and Microsoft Excel. A CSV file co
4 min read
How to convert CSV File to PDF File using Python?
In this article, we will learn how to do Conversion of CSV to PDF file format. This simple task can be easily done using two Steps : Firstly, We convert our CSV file to HTML using the PandasIn the Second Step, we use PDFkit Python API to convert our HTML file to the PDF file format. Approach: 1. Con
3 min read
How to Sort data by Column in a CSV File in Python ?
In this article, we will discuss how to sort CSV by column(s) using Python. Method 1: Using sort_values() We can take the header name as per our requirement, the axis can be either 0 or 1, where 0 means 'rows' and '1' means 'column'. Ascending can be either True/False and if True, it gets arranged i
3 min read
Creating a dataframe using CSV files
CSV (Comma-Separated Values) files are widely used in data science for storing tabular data, similar to Excel sheets. In Python the Pandas library is essential for handling and analyzing large datasets stored in CSV format. Below are three methods to create a Pandas DataFrame from a CSV file:Method
2 min read
Reading and Writing CSV Files in Python
CSV (Comma Separated Values) format is one of the most widely used formats for storing and exchanging structured data between different applications, including databases and spreadsheets. CSV files store tabular data, where each data field is separated by a delimiter, typically a comma. Python provi
4 min read