摘要:python 对数据的排序,绘图
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
PATH='F:\\课程\\Python数据分析-pandas玩转Excel\\资料\\007-008\\List.xlsx'
PATH1='F:\\课程\\Python数据分析-pandas玩转Excel\\资料\\007-008\\Students.xlsx'
# 排序测试
def read_excel():
data=pd.read_excel(PATH,index_col='ID')
print(data.head(5))
print("=====================")
data.sort_values(by=['Worthy','Price'],ascending=[True,True],inplace=True)
print(data.head(10))
# 按条件查询
def read_excel1():
students=pd.read_excel(PATH1,index_col='ID')
print(students.head(8))
print("==========================")
students=students.loc[students['Age'].apply(lambda x:18 <= x <= 30)].loc[students['Score'].apply(lambda x:80<=x<=100)]
print(students)
PATH2='F:\\课程\\Python数据分析-pandas玩转Excel\\资料\\009\\Students.xlsx'
# 按条件绘图
def read_excel_plot():
students=pd.read_excel(PATH2)
students.sort_values(by="Number",inplace=True,ascending=False) # 从大到小
print(students.head(10))
students.plot.bar(x='Field',y='Number',color='blue',title='International Student by Field Number')
plt.tight_layout()
plt.show()
# 按条件绘图
def read_excel_plot1():
students=pd.read_excel(PATH2)
students.sort_values(by="Number",inplace=True,ascending=False) # 从大到小
print(students.head(10))
plt.bar(students['Field'],students['Number'],color='orange',width=0.7)
# plt.xticks(students['Field'],rotation=123)
plt.title('International Student by Field',fontsize=16)
plt.xlabel('Field')
plt.ylabel('Number')
ax=plt.gca()
ax.set_xticklabels(students['Field'],rotation=40,ha='right')
plt.tight_layout()
plt.show()
PATH3='F:\\课程\\Python数据分析-pandas玩转Excel\\资料\\010\\Students.xlsx'
def read_excel_plot2():
students=pd.read_excel(PATH3)
students.sort_values(by='2017',inplace=True,ascending=False)
print(students.head(5))
bar_width=0.5
x_pos=np.arange(len(students['2017'])*2,step=2)
plt.bar(x_pos,students['2016'],width=0.5,color='orange')
plt.bar(x_pos+bar_width,students['2017'],width=0.5,color='red')
plt.xlabel('Filed')
plt.ylabel('Number')
plt.tight_layout()
plt.show()
if __name__ == '__main__':
read_excel_plot2()