python 实现对数据的排序和绘图

 摘要: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()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值