pandas操作excel-16-数据处理

99封情书 提交于 2020-03-01 21:15:45

拆分单元格内容

import pandas as pd

employees = pd.read_excel('C:/employee.xlsx')
df = employees['Full Name'].str.split(n=2, expand=True)

employees['First Name'] = df[0]
employees['Last Name'] = df[1]

数据统计,统计每个学员的总分,平均分,所有学员各科平均分,总分

students = pd.read_excel('C:/students.xlsx')

temp = students[['Test1', 'Test2', 'Test3']]
row_sum = temp.sum(axis=1)
row_mean = temp.mean(axis=1)
students['Total'] = row_sum
students['Average'] = row_mean

col_mean = students[['Test1', 'Test2', 'Test3', 'Total', 'Average']].mean(axis=0)
col_mean['Name'] = 'Summary'
students.append(col_mean, ignore_index=True)

去除重复数据

students.drop_duplicates(subset='Name')

找出重复数据

# 根据Name 查找是否有重复行,重复的话dupe值为True,否则为False
dupe = students.duplicated(subset='Name')
# 获取重复的行
dupe = dupe[dupe==True]
# 找到重复数据
dupe_data = students.iloc[dupe.index]

旋转表格,行转列,列转行

table = video.transpose()

读取CSV,TXT数据

table = pd.read_csv('C:/data.csv', sep='\t', index_col='ID')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!