A.#导入数据包

  1. In [1]: import numpy as np
  2. In [2]: import pandas as pd

由 pandas 定义的两种数据类型是 Series(一维数据) 和 DataFrame(二维数据),Series 类似一个 column,DataFrame 是多个 Series 的集合。
B.Series

1.定义一个Series s = pd.Series(np.random.randon.randon(5), name =”菜j学python” ) s 0 2.138318 1 -0.735571 2 -0.814823 3 -1.071135 4 0.054460 name:菜j学习python ,dytple:float64
注:np.random.randn()函数随机返回一个或一组样本,具有标准正态分布 2.Series乘以10 s*10 0 21.38318 1 -7.35571 2 -8.14823 3 -10.71135 4 0.54460 name:”菜j学习python” ,dytple:float64 3.Series取绝对值 np.abs(s) 0 2.138318 1 0.735571 2 0.814823 3 1.071135 4 0.054460 name:菜j学习python ,dytple:float64

对Series做描述性统计

s.describle() 注:abs()函数返回数字的绝对值 4.对Series自定义索引值 s.index=[“菜”,”j”,”学”,”习”,”python”] s
菜 2.138318 j -0.735571 学 -0.814823 习 -1.071135 python 0.054460 name:菜j学习python ,dytple:float64 5.对Series做查询(类似字典) s[“python”] 0.5445991095251599 6.将python索引值改为2 s[“python”]=2 s 菜 2.138318 j -0.735571 学 -0.814823 习 -1.071135 python 2.000000 name:菜j学习python ,dytple:float64 7.判断索引j是否存在 “j” in s Ture

C.DateFrame
1.读取数据
df=pd.read_excel(“GDP.xlsx”)
type(df)
:pandas.core.frame.DateFrame
:#获取前五行
df.read(5)
2.获取前五行
df.read(5)
image.png
3.获取前3-4行
df[2:4]
image.png
4.获取特定的列
df.[[“province”,”2019_gdp”]].head.(5)
image.png
5.使用整数选择行和列
df.iloc(0:3,0:3)
image.png
备注:通过iloc属性实现
6.使用整数和标签的混合来选择行和列
df.loc[df.inde(0:3),[“province”,”2019_gdp”]]
image.png
备注:通过loc属性实现
7.获取这三列
df=df[[“province”,”2010_gdp”,”2019_gdp”]]
df.head(5)
image.png
8.重置索引为province
df=df.set_index(“province”)
df.head(5)
image.png
9.更改列名
df.columns =”2010”,”2019”
image.png
10.对数据进行计算
df[“2010”] =df[“2010”]/le4
df[“2019”] =df[“2019”]/le4
df[“increase”]=(df[“2019”]-df[“2010”] )/df[“2010”]
df.head[5]
image.png
备注:le4表示10000
11.matplotlib作图
1.解决中文乱码问题
image.png
2以2019年各省gdp总额做柱状图
import.matplotilb.pyplot.as plt
df[“2019”].plot(kind=”bar”)
plt.rcParams[“font.sans-serif”]=[“SimHei”]#设置加载的字体名
plt.rcParams[“axes.unicode_minus”]=Flase#解决保存图像是负号”-“显示为方块的问题
plt.show()
image.png
3.根据increase字段降序
df=df.sort_valus(by=”increase”,ascending=False)
df.head(5)
image.png
4.以2019年较2010年GDP增幅做柱状图
df[“increase”].plot(kind=”bar”)
plt.title(“2019年较2010年GDP增幅”)#添加标题
plt.yalble(”gdp_increase”)#添加纵坐标标签
plt.show()
image.png