“”"
@theme pandas
@author lz
@time 2019/01/03
@content 读取xls文件,并修改
io:excel文件,如果命名为中文,在python2.7中,需要使用decode()来解码成unicode字符串,例如: pd.read_excel(‘示例’.decode('utf-8))
sheet_name:返回指定的sheet,如果将sheet_name指定为None,则返回全表,如果需要返回多个表,可以将sheet_name指定为一个列表,例如[‘sheet1’, ‘sheet2’]
header:指定数据表的表头,默认值为0,即将第一行作为表头。
usecols:读取指定的列,例如想要读取第一列和第二列数据:
pd.read_excel("example.xlsx", sheet_name=None, usecols=[0, 1])
“”"
coding:utf-8
import pandas as pd
import xlwt
import xlrd
df=pd.read_excel(“example.xls”)
print(df)
#修改值,但不修改原来的excel文件
df[‘gender’][0]=“male”
df[‘gender’][1]=“female”
print(df)
#增加行数据
df.loc[3]=[‘bob’,33,‘male’,1,0]
#增加列数据
df[‘houfou’]=0
#保存修改
print(df)
#删除指定列利用axis=1
df01=df.drop(‘gender’,axis=1)
#删除指定行
df02=df.drop([2,3],axis=0)
print(df01)
print(df02)
#保存
pd.DataFrame(df).to_excel(‘