Python之pandas
环境:jupyter Notebook(Anaconda)
1.引入pandas库和numpy库
import pandas as pd
import numpy as np
2. 读取文件
t1=pd.read_excel('D:\scores.xlsx',header=[0,1],index_col=0)
t1

3. index函数
index 接收boolean。表示是否将行索引作为数据传入数据库。默认为True index_label 接收string或者sequence.代表是否引用索引名称,如果index参数为true,此参数为None,则使用默认名称。如果为多重索引,则必须使用sequence形式。默认为None
sorted_obj=t1.sort_index(ascending=True)
sorted_obj

4.max与min函数
sorted_obj.max()
sorted_obj.min()
tt=sorted_obj['一本分数线','文科'].max()-sorted_obj['一本分数线','文科'].min()
tt
 
5. ptp函数
np.ptp()函数实现的功能等同于np.max(array) - np.min(array) 调用ptp()函数计算极差。
result1=np.array(sorted_obj['一本分数线','文科']).ptp()
result1
result2=np.array(sorted_obj['一本分数线','理科']).ptp()
result2
result3=np.array(sorted_obj['二本分数线','文科']).ptp()
result3
result4=np.array(sorted_obj['二本分数线','理科']).ptp()
result4

6. sorted函数
比较2018年一本与二本文理科分数线的差值
ser_obj1=sorted_obj['一本分数线','文科']
ser_obj1[2018]-ser_obj1[2017]
ser_obj1=sorted_obj['一本分数线','理科']
ser_obj1[2018]-ser_obj1[2017]
ser_obj1=sorted_obj['二本分数线','理科']
ser_obj1[2018]-ser_obj1[2017]
ser_obj1=sorted_obj['二本分数线','文科']
ser_obj1[2018]-ser_obj1[2017]

7. describe函数
mean()函数或describe()函数都可以计算出每列的平均数,通过调用describe()方法来查看多个统计指标
计算2006-2018年的平均分数线
sorted_obj.describe()

 码字不易,觉得内容有帮助的请一键三联哟~ 这样我也才有动力和你一起学习
|