当前位置: 首页 > 后端技术 > Python

Pandas入门教程(五)

时间:2023-03-25 21:22:11 Python

将pandas导入为pdimportnumpy作为np#折线图s=pd.Series(np.random.randn(10),index=np.arange(0,100,10))s.plot()df=pd.DataFrame(np.random.randn(10,4),index=np.arange(0,100,10),columns=list('ABCD'))print(df)df.plot()ABCD0-0.3584991.242513-1.2002300.21805110-1.540113-0.344976-1.5094151.162260200.6809610.398406-0.4298561.027732300.545883-0.1011281.7479350.423187400.763575-0.6826980.8859150.79683550-2.419253-0.794231-1.100932-0.309591601.2120270.684612-0.0356150.999927700.362055-1.8512970.8601201.27199680-1.199978-1.1589610.644087-0.840503902.0334640.301777-0.629141-0.702707importmatplotlib.pyplotasplts=pd.Series(np.random.randn(16),index=list('abcdefghijklmnop'))fig,aexs=plt.subplots(2,1)#垂直列图s.plot(ax=aexs[0],kind='bar')#水平直方图s.plot(ax=aexs[1],kind='barh')#堆叠直方图df=pd.DataFrame(np.random.randn(10,4),index=np.arange(0,100,10),columns=list('ABCD'))df.plot(kind='bar',stacked=True)plt.show()#hist直方图df=pd.DataFrame({'A':np.random.randn(1000),'B':np.random.randn(1000)})df.plot(kind='hist',bins=500)plt.show()df2=pd.DataFrame({'C':np.random.randn(1000)})df3=pd.concat([df,df2],axis=1)print(df3)ABC0-0.8579161.603299-0.5848071-0.192799-0.8249721.1386442-1.780869-0.5013841.31810430.916919-0.5645780.8328444-0.153033-0.381179-0.122554...........9950.188451-0.7635630.2304789960.371608-0.9463060.3189049970.299794-0.4207490.276406998-0.193156-0.091357-0.9260479990.8716531.204641-1.030529[1000行x3列]#散点图data=df3[['A','B','C']]data.plot.scatter('A','B')pd.plotting.scatter_matrix(data,color='g',alpha=0.3)array([[,,],[,,],[,,]],dtype=object)