DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Configure AWS Glue Job Using Python-Based AWS CDK
  • Using Python Libraries in Java
  • Start Coding With Google Cloud Workstations
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning

Trending

  • Blue Skies Ahead: An AI Case Study on LLM Use for a Graph Theory Related Application
  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • AI's Dilemma: When to Retrain and When to Unlearn?
  • Top Book Picks for Site Reliability Engineers
  1. DZone
  2. Coding
  3. Languages
  4. Splitting CSV Files in Python

Splitting CSV Files in Python

In this article, see a code snippet that splits CSV files in Python.

By 
Zehra Can user avatar
Zehra Can
·
Oct. 08, 20 · Code Snippet
Likes (5)
Comment
Save
Tweet
Share
70.4K Views

Join the DZone community and get the full member experience.

Join For Free

Sometimes it is necessary to split big files into small ones. In this brief article, I will share a small script which is written in Python. Maybe you can develop it further. However, if the file size is bigger you should be careful using loops in your code.

Let's assume your input file name input.csv. The code will split it into new files with names input_1.csv, ..., input_10.csv.,...

Python
 




x


 
1
import pandas as pd
2
 
          
3
#csv file name to be read in 
4
in_csv = 'input.csv'
5
 
          
6
#get the number of lines of the csv file to be read
7
number_lines = sum(1 for row in (open(in_csv)))
8
 
          
9
#size of rows of data to write to the csv, 
10
#you can change the row size according to your need
11
rowsize = 500
12
 
          
13
#start looping through data writing it to a new file for each set
14
for i in range(1,number_lines,rowsize):
15
    df = pd.read_csv(in_csv,
16
          header=None,
17
          nrows = rowsize,#number of rows to read at each loop
18
          skiprows = i)#skip rows that have been read
19
 
          
20
    #csv to write data to a new file with indexed name. input_1.csv etc.
21
    out_csv = 'input' + str(i) + '.csv'
22
 
          
23
    df.to_csv(out_csv,
24
          index=False,
25
          header=False,
26
          mode='a',#append data to csv file
27
          chunksize=rowsize)#size of data to append for each loop



Python (language) CSV

Opinions expressed by DZone contributors are their own.

Related

  • How to Configure AWS Glue Job Using Python-Based AWS CDK
  • Using Python Libraries in Java
  • Start Coding With Google Cloud Workstations
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!