pandas之excel

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')

遇到问题:

  1. NameError: name ‘pandas’ is not defined?

    这是没有pandas模块,这是因为python默认没有安装numpy和pandas,最直接的方法是在python的scripts中执行pip install pandas不过,据说用pip安装会损坏原文件。

  2. 或者在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)) #打印出前3行,head不加参数默认为5行
print(people.tail(3))

df = pd.read_excel('path',index_col='ID') #后面的参数是为了不要将系统自动添加的索引加到新的表中
df.to_excel('path')
文章目录
  1. 1. pandas
    1. 1.0.1. 创建文件
    2. 1.0.2. 打开文件
|