python的pandas玩转excel

孔海超
2023-12-01

从昨天开始在网易云课堂学习 Python数据分析-pandas玩转Excel 课程,python玩转excel,视频中所用资料在蔓藤教育

  1. excel读取写入
def readExcel():
    people = pd.read_excel('D:/study/pythonTest/people.xlsx')
    print(people.shape)#行数 列数
    print(people.columns)#获取列名
    print(people.head(3))#获取前3行数据
    print('-------------------------------')
    print(people.tail(3))#获取后3行数据

def writeExcel1():
    people = pd.read_excel('D:/study/pythonTest/people.xlsx',header=None) #不读表头
    people.columns = ['ID','Type','Title','FirstName','MiddleName','LastName'] #增加表头
    people.set_index('ID', inplace=True) #把ID当作索引,舍掉dataFrame的索引
    print(people.columns)  
    people.to_excel('D:/study/pythonTest/output.xlsx')
    print('Done!!')

df = pd.read_excel('D:/study/pythonTest/output.xlsx', index_col='ID')#读取时就将ID设置为索引
print(df.head(5))

2019年9月2日,今天看到python操作的一篇文章,pandas的数据选取详解

 类似资料: