PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, August 27, 2022

[FIXED] How to extract each sheet within an Excel file into an individual csv file as it is without appending any column or row?

 August 27, 2022     csv, excel, pandas, python     No comments   

Issue

enter image description hereI'm trying to extract each sheet in an Excel file into multiple CSVs. For example Sheet1, Sheet2 and Sheet3 from Sample_1.xlsx into Sheet1.csv, Sheet2.csv and Sheet3.csv The code I run is as below :

import pandas as pd

dfs = pd.read_excel('Sample_1.xlsx', sheet_name=None)

for sheet_name, data in dfs.items():
    data.to_csv(f"{sheet_name}.csv")

It outputs all of the three CSVs as desired but each csv has an extra column (column A) with index starting 0,1,2..n . Why is that happening? and How do I get rid of it?


Solution

You need to specify the index parameter of pandas.DataFrame.to_csv as False

Replace :

data.to_csv(f"{sheet_name}.csv")

By :

data.to_csv(f"{sheet_name}.csv", index=False)


Answered By - L'Artiste
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing