pandas
创建文件
1 2 3 4 5
| import pandas as pd df = pd.DataFrame({'ID':[1,2,3],'Name':['Tim','Victor','Nick']}) df = df.set_index('ID') df.to_excel('F:/webProject/python/output.xlsx') print('Done')
|
遇到问题:
NameError: name ‘pandas’ is not defined?
这是没有pandas模块,这是因为python默认没有安装numpy和pandas,最直接的方法是在python的scripts中执行pip install pandas
不过,据说用pip安装会损坏原文件。
或者在Spyder编辑器中安装pip install pandas
,却报Note: you may need to restart the kernel to use updated packages.
?
在Terminal中更新conda update spyder
。
打开文件
1 2 3 4 5 6 7 8 9 10
| import pandas as pd people = pd.read_execl('path',header=None) people.set_index('ID',inplace=True) print(people.shape) print(people.columns) print(people.head(3)) print(people.tail(3))
df = pd.read_excel('path',index_col='ID') df.to_excel('path')
|