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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Configure AWS Glue Job Using Python-Based AWS CDK
  • Building a Product Recommendation Engine With Neo4j — No ML Library Required
  • 6 Techniques To Reduce LLM API Costs With the Python Library
  • How to Design a Multi-Agent AI Framework in Python for Enterprise LLM Workflows

Trending

  • AWS Bedrock vs Vertex AI vs Azure Foundry: Stop Comparing Benchmarks, Start Asking This Instead
  • How a 30B Model Runs on Your Laptop
  • Classification Never Left. It Just Got a New Home in LLMs.
  • Grounding AI Agents in Governed Data
  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
71.5K 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
  • Building a Product Recommendation Engine With Neo4j — No ML Library Required
  • 6 Techniques To Reduce LLM API Costs With the Python Library
  • How to Design a Multi-Agent AI Framework in Python for Enterprise LLM Workflows

Partner Resources

×

Comments

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

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook