Python Pandas: Read and Write Excel (.xlsx) Files
To read an xlsx file using pandas in Python, you can use the read_excel() function. Here's an example:
import pandas as pd
# read the xlsx file
df = pd.read_excel('filename.xlsx', sheet_name='Sheet1')
# display the data
print(df)
To write an xlsx file using pandas in Python, you can use the to_excel() function. Here's an example:
import pandas as pd
# create a dataframe
df = pd.DataFrame({'Name': ['John', 'Lisa', 'Mike'],
'Age': [25, 30, 35],
'Salary': [50000, 70000, 90000]})
# write the dataframe to an xlsx file
df.to_excel('output.xlsx', sheet_name='Sheet1', index=False)
# confirm that the file was created
import os
os.path.isfile('output.xlsx') # should return True
In this example, the to_excel() function writes the dataframe df to an xlsx file named output.xlsx in the current working directory. The index=False argument tells pandas not to include the row index in the output.
原文地址: https://www.cveoy.top/t/topic/lCyj 著作权归作者所有。请勿转载和采集!