SoFunction
Updated on 2024-11-10

pandas how to save data to excel,csv

pandas save data to excel,csv

pandas is simpler to save data

For any dataframe:

import pandas as pd
import numpy as np
 
dataframe = (data=(size=(10, 10)))

Import to excel

dataframe.to_excel("File.xlsx")

Import to csv

dataframe.to_csv("File.csv")

More detailed operation

More parameters can be added, for example:

dataframe.to_excel("File.xlsx", index=False, header=None)
  • index=FalseThe leftmost column is the one that doesn't export the index.
  • header=NoneThis means that the first row, the column header, will not be exported.

For csv, there are also common operations:

dataframe.to_csv("File.csv",sep=',')

sep sets the separator so that each column will be separated by a ",", if it is changed to sep="$" then the two data will be separated by the $ sign

The most basic operations for saving data to csv or xlsx

pandas provides very handy functions to save data to cvs or xlsx.

import pandas as pd
import numpy as np
import pymysql
from sqlalchemy import create_engine
import openpyxl
pdata = pd.read_csv('')
df = pdata.to_csv('')
print(df)

Here I first read the data in the pdata variable and then use the

pdata.to_csv('') saves the data in a '' file.

Only this one parameter is described here. If only the filename is filled in, it defaults to the directory of that python file, or you can use an absolute address.

If this parameter is not filled, df returns pdata data of type string.

If this parameter is filled, pd returns None.

pdata.to_excel('',sheet_name="sheetname",index=False)
  • The first parameter is the name of the saved file, note that it cannot be null.
  • sheet_name Setting up excel file footnotes
  • index=False This means that the index is not written to the file