pandas is a package
How to use pandas to create an Excel file?
DataFrame(数据帧)
DataFrame is a very important Class in pandas, a DataFrame can be treated as a worksheet in Excel. Take care of the first letter "D" is upper case.
import pandas as pd
df = pd.DataFrame({'ID':[1,2,3],'Name':['Tim','Victor','Nick']})
df = df.set_index('ID')
print(df)
df.to_excel('C:/Temp/output.xlsx')
print('Done!')
'ID' and 'Name' is the name of the column.
[1,2,3] and ['Tim','Victor','Nick'] is the record under column ID and Name.
Code:
DataFrame.set_index('ID')
is set the column ID as the index. otherwise, the DataFrame will add an additional column as the index.
Code:
DataFram.to_excel('C:/Temp/output.xlsx')
.to_excel is a method of Class DataFrame. that will create an excel file under an absolute address.
Result:
Name
ID
1 Tim
2 Victor
3 Nick
Done!
Process finished with exit code 0