时间:2021-05-22
本文实例讲述了Python数据分析模块pandas用法。分享给大家供大家参考,具体如下:
一 介绍
pandas(Python Data Analysis Library)是基于numpy的数据分析模块,提供了大量标准数据模型和高效操作大型数据集所需要的工具,可以说pandas是使得Python能够成为高效且强大的数据分析环境的重要因素之一。
pandas主要提供了3种数据结构:
1)Series,带标签的一维数组。
2)DataFrame,带标签且大小可变的二维表格结构。
3)Panel,带标签且大小可变的三维数组。
二 代码
1、生成一维数组
>>>import pandas as pd>>>import numpy as np>>> x = pd.Series([1,3,5, np.nan])>>>print(x)01.013.025.03NaNdtype: float642、生成二维数组
>>> dates = pd.date_range(start='20170101', end='20171231', freq='D')#间隔为天>>>print(dates)DatetimeIndex(['2017-01-01','2017-01-02','2017-01-03','2017-01-04','2017-01-05','2017-01-06','2017-01-07','2017-01-08','2017-01-09','2017-01-10',...'2017-12-22','2017-12-23','2017-12-24','2017-12-25','2017-12-26','2017-12-27','2017-12-28','2017-12-29','2017-12-30','2017-12-31'],dtype='datetime64[ns]', length=365, freq='D')>>> dates = pd.date_range(start='20170101', end='20171231', freq='M')#间隔为月>>>print(dates)DatetimeIndex(['2017-01-31','2017-02-28','2017-03-31','2017-04-30','2017-05-31','2017-06-30','2017-07-31','2017-08-31','2017-09-30','2017-10-31','2017-11-30','2017-12-31'],dtype='datetime64[ns]', freq='M')>>> df = pd.DataFrame(np.random.randn(12,4), index=dates, columns=list('ABCD'))>>>print(df)A B C D2017-01-31-0.6825560.2441020.4508550.2364752017-02-28-0.6300600.5906670.4824380.2256972017-03-311.0669890.3193391.0949531.7160532017-04-300.334944-0.053049-1.009493-1.0394702017-05-31-0.380778-0.0444290.0756470.9312432017-06-300.8675400.872197-0.738974-1.1145962017-07-310.423371-1.0863860.183820-0.4389212017-08-311.2851630.634134-0.4729731.2810572017-09-30-1.002832-0.888122-1.316014-0.0706372017-10-311.735617-0.2538150.5544031.5362112017-11-302.0303840.6675561.0126980.2394792017-12-312.059718-0.0890501.4205170.224578>>> df = pd.DataFrame([[np.random.randint(1,100)for j in range(4)]for i in range(12)], index=dates, columns=list('ABCD'))>>>print(df)A B C D2017-01-3175325222017-02-28709970982017-03-31994775672017-04-30337017492017-05-31628868912017-06-30197518442017-07-31508565822017-08-3156287762017-09-3061731112017-10-3182966922017-11-3063591942017-12-3179586933>>> df = pd.DataFrame({'A':[np.random.randint(1,100)for i in range(4)],'B':pd.date_range(start='20130101', periods=4, freq='D'),'C':pd.Series([1,2,3,4],index=list(range(4)),dtype='float32'),'D':np.array([3]*4,dtype='int32'),'E':pd.Categorical(["test","train","test","train"]),'F':'foo'})>>>print(df)A B C D E F0152013-01-011.03 test foo1112013-01-022.03 train foo2912013-01-033.03 test foo3912013-01-044.03 train foo>>> df = pd.DataFrame({'A':[np.random.randint(1,100)for i in range(4)],'B':pd.date_range(start='20130101', periods=4, freq='D'),'C':pd.Series([1,2,3,4],index=['zhang','li','zhou','wang'],dtype='float32'),'D':np.array([3]*4,dtype='int32'),'E':pd.Categorical(["test","train","test","train"]),'F':'foo'})>>>print(df)A B C D E Fzhang 362013-01-011.03 test fooli 862013-01-022.03 train foozhou 102013-01-033.03 test foowang 792013-01-044.03 train foo>>>3、二维数据查看
>>> df.head() #默认显示前5行A B C D E Fzhang 362013-01-011.03 test fooli 862013-01-022.03 train foozhou 102013-01-033.03 test foowang 792013-01-044.03 train foo>>> df.head(3) #查看前3行A B C D E Fzhang 362013-01-011.03 test fooli 862013-01-022.03 train foozhou 102013-01-033.03 test foo>>> df.tail(2) #查看最后2行A B C D E Fzhou 102013-01-033.03 test foowang 792013-01-044.03 train foo4、查看二维数据的索引、列名和数据
>>> df.indexIndex(['zhang','li','zhou','wang'], dtype='object')>>> df.columnsIndex(['A','B','C','D','E','F'], dtype='object')>>> df.valuesarray([[36,Timestamp('2013-01-01 00:00:00'),1.0,3,'test','foo'],[86,Timestamp('2013-01-02 00:00:00'),2.0,3,'train','foo'],[10,Timestamp('2013-01-03 00:00:00'),3.0,3,'test','foo'],[79,Timestamp('2013-01-04 00:00:00'),4.0,3,'train','foo']], dtype=object)5、查看数据的统计信息
>>> df.describe() #平均值、标准差、最小值、最大值等信息A C Dcount 4.0000004.0000004.0mean 52.7500002.5000003.0std 36.0682221.2909940.0min 10.0000001.0000003.025%29.5000001.7500003.050%57.5000002.5000003.075%80.7500003.2500003.0max 86.0000004.0000003.06、二维数据转置
>>> df.Tzhang li zhou \A 368610B 2013-01-0100:00:002013-01-0200:00:002013-01-0300:00:00C 123D 333E test train testF foo foo foowangA 79B 2013-01-0400:00:00C 4D 3E trainF foo7、排序
>>> df.sort_index(axis=0, ascending=False)#对轴进行排序A B C D E Fzhou 102013-01-033.03 test foozhang 362013-01-011.03 test foowang 792013-01-044.03 train fooli 862013-01-022.03 train foo>>> df.sort_index(axis=1, ascending=False)F E D C B Azhang foo test 31.02013-01-0136li foo train 32.02013-01-0286zhou foo test 33.02013-01-0310wang foo train 34.02013-01-0479>>> df.sort_index(axis=0, ascending=True)A B C D E Fli 862013-01-022.03 train foowang 792013-01-044.03 train foozhang 362013-01-011.03 test foozhou 102013-01-033.03 test foo>>> df.sort_values(by='A')#对数据进行排序A B C D E Fzhou 102013-01-033.03 test foozhang 362013-01-011.03 test foowang 792013-01-044.03 train fooli 862013-01-022.03 train foo>>> df.sort_values(by='A', ascending=False)#降序排列A B C D E Fli 862013-01-022.03 train foowang 792013-01-044.03 train foozhang 362013-01-011.03 test foozhou 102013-01-033.03 test foo8、数据选择
>>> df['A']#选择列zhang 1li 1zhou 60wang 58Name: A, dtype: int64>>> df[0:2]#使用切片选择多行A B C D E Fzhang 12013-01-011.03 test fooli 12013-01-022.03 train foo>>> df.loc[:,['A','C']]#选择多列A Czhang 11.0li 12.0zhou 603.0wang 584.0>>> df.loc[['zhang','zhou'],['A','D','E']]#同时指定多行与多列进行选择A D Ezhang 13 testzhou 603 test>>> df.loc['zhang',['A','D','E']]A 1D 3E testName: zhang, dtype: object9、数据修改和设置
>>> df.iat[0,2]=3#修改指定行、列位置的数据值>>>print(df)A B C D E Fzhang 12013-01-013.03 test fooli 12013-01-022.03 train foozhou 602013-01-033.03 test foowang 582013-01-044.03 train foo>>> df.loc[:,'D']=[np.random.randint(50,60)for i in range(4)]#修改某列的值>>>print(df)A B C D E Fzhang 12013-01-013.057 test fooli 12013-01-022.052 train foozhou 602013-01-033.057 test foowang 582013-01-044.056 train foo>>> df['C']=-df['C']#对指定列数据取反>>>print(df)A B C D E Fzhang 12013-01-01-3.057 test fooli 12013-01-02-2.052 train foozhou 602013-01-03-3.057 test foowang 582013-01-04-4.056 train foo10、缺失值处理
>>> df1 = df.reindex(index=['zhang','li','zhou','wang'], columns=list(df.columns)+['G'])>>>print(df1)A B C D E F Gzhang 12013-01-01-3.057 test foo NaNli 12013-01-02-2.052 train foo NaNzhou 602013-01-03-3.057 test foo NaNwang 582013-01-04-4.056 train foo NaN>>> df1.iat[0,6]=3#修改指定位置元素值,该列其他元素为缺失值NaN>>>print(df1)A B C D E F Gzhang 12013-01-01-3.057 test foo 3.0li 12013-01-02-2.052 train foo NaNzhou 602013-01-03-3.057 test foo NaNwang 582013-01-04-4.056 train foo NaN>>> pd.isnull(df1)#测试缺失值,返回值为True/False阵列A B C D E F Gzhang FalseFalseFalseFalseFalseFalseFalseli FalseFalseFalseFalseFalseFalseTruezhou FalseFalseFalseFalseFalseFalseTruewang FalseFalseFalseFalseFalseFalseTrue>>> df1.dropna()#返回不包含缺失值的行A B C D E F Gzhang 12013-01-01-3.057 test foo 3.0>>> df1['G'].fillna(5, inplace=True)#使用指定值填充缺失值>>>print(df1)A B C D E F Gzhang 12013-01-01-3.057 test foo 3.0li 12013-01-02-2.052 train foo 5.0zhou 602013-01-03-3.057 test foo 5.0wang 582013-01-04-4.056 train foo 5.011、数据操作
>>> df1.mean()#平均值,自动忽略缺失值A 30.0C -3.0D 55.5G 4.5dtype: float64>>> df.mean(1)#横向计算平均值zhang 18.333333li 17.000000zhou 38.000000wang 36.666667dtype: float64>>> df1.shift(1)#数据移位A B C D E F Gzhang NaNNaTNaNNaNNaNNaNNaNli 1.02013-01-01-3.057.0 test foo 3.0zhou 1.02013-01-02-2.052.0 train foo 5.0wang 60.02013-01-03-3.057.0 test foo 5.0>>> df1['D'].value_counts()#直方图统计572561521Name: D, dtype: int64>>>print(df1)A B C D E F Gzhang 12013-01-01-3.057 test foo 3.0li 12013-01-02-2.052 train foo 5.0zhou 602013-01-03-3.057 test foo 5.0wang 582013-01-04-4.056 train foo 5.0>>> df2 = pd.DataFrame(np.random.randn(10,4))>>>print(df2)01230-0.939904-1.856658-0.2819650.20362410.3501620.060674-0.9148080.1357352-1.031384-1.6112740.341546-0.36367130.139464-0.050959-0.810610-0.7726484-1.146810-0.7916081.488790-0.4900045-0.100707-0.763545-0.071274-0.2981426-0.2120140.8097090.6931960.9805687-0.812985-0.000325-0.675101-0.21739480.066969-0.084609-0.4330990.5356169-0.319120-0.5328541.321712-1.751913>>> p1 = df2[:3] >>> print(p1) 0 1 2 3 0 -0.939904 -1.856658 -0.281965 0.203624 1 0.350162 0.060674 -0.914808 0.135735 2 -1.031384 -1.611274 0.341546 -0.363671 >>> p2 = df2[3:7] >>> print(p2) 0 1 2 3 3 0.139464 -0.050959 -0.810610 -0.772648 4 -1.146810 -0.791608 1.488790 -0.490004 5 -0.100707 -0.763545 -0.071274 -0.298142 6 -0.212014 0.809709 0.693196 0.980568 >>> p3 = df2[7:] >>> print(p3) 0 1 2 3 7 -0.812985 -0.000325 -0.675101 -0.217394 8 0.066969 -0.084609 -0.433099 0.535616 9 -0.319120 -0.532854 1.321712 -1.751913 >>> df3 = pd.concat([p1, p2, p3]) #数据行合并 >>> print(df3) 0 1 2 3 0 -0.939904 -1.856658 -0.281965 0.203624 1 0.350162 0.060674 -0.914808 0.135735 2 -1.031384 -1.611274 0.341546 -0.363671 3 0.139464 -0.050959 -0.810610 -0.772648 4 -1.146810 -0.791608 1.488790 -0.490004 5 -0.100707 -0.763545 -0.071274 -0.298142 6 -0.212014 0.809709 0.693196 0.980568 7 -0.812985 -0.000325 -0.675101 -0.217394 8 0.066969 -0.084609 -0.433099 0.535616 9 -0.319120 -0.532854 1.321712 -1.751913 >>> df2 == df3 0 1 2 3 0 True True True True 1 True True True True 2 True True True True 3 True True True True 4 True True True True 5 True True True True 6 True True True True 7 True True True True 8 True True True True 9 True True True True >>> df4 = pd.DataFrame({'A':[np.random.randint(1,5) for i in range(8)], 'B':[np.random.randint(10,15) for i in range(8)], 'C':[np.random.randint(20,30) for i in range(8)], 'D':[np.random.randint(80,100) for i in range(8)]}) >>> print(df4) A B C D 0 4 11 24 91 1 1 13 28 95 2 2 12 27 91 3 1 12 20 87 4 3 11 24 96 5 1 13 21 99 6 3 11 22 95 7 2 13 26 98 >>> >>> df4.groupby('A').sum() #数据分组计算 B C D A 1 38 69 281 2 25 53 189 3 22 46 191 4 11 24 91 >>> >>> df4.groupby(['A','B']).mean() C D A B 1 12 20.0 87.0 13 24.5 97.0 2 12 27.0 91.0 13 26.0 98.0 3 11 23.0 95.5 4 11 24.0 91.012、结合matplotlib绘图
>>>import pandas as pd>>>import numpy as np>>>import matplotlib.pyplot as plt>>> df = pd.DataFrame(np.random.randn(1000,2), columns=['B','C']).cumsum()>>>print(df)B C00.0898860.51108111.3237661.58475821.489479-0.43867130.831331-0.3980214-0.2482330.4944185-0.0130850.68451860.666951-1.42216171.768838-0.65878682.6610800.64850591.9517510.836261103.5387851.657475113.2540342.052609124.2486201.568401134.0771730.055622143.452590-0.200314152.627620-0.408829163.690537-0.210440173.1849240.365447183.646556-0.150044194.164563-0.023405202.3914470.517872212.8651530.686649223.6231830.663927231.5451170.151044243.5959240.903619253.0138041.855083264.4388011.014572275.1552160.882628284.4314570.741509292.8419490.709991........970-7.910646-13.738689971-7.318091-14.811335972-9.144376-15.466873973-9.538658-15.367167974-9.061114-16.822726975-9.803798-17.368350976-10.180575-17.270180977-10.601352-17.671543978-10.804909-19.535919979-10.397964-20.361419980-10.979640-20.300267981-8.738223-20.202669982-9.339929-21.528973983-9.780686-20.902152984-11.072655-21.235735985-10.849717-20.439201986-10.953247-19.708973987-13.032707-18.687553988-12.984567-19.557132989-13.508836-18.747584990-13.420713-19.883180991-11.718125-20.474092992-11.936512-21.360752993-14.225655-22.006776994-13.524940-20.844519995-14.088767-20.492952996-14.169056-20.666777997-14.798708-19.960555998-15.766568-19.395622999-17.281143-19.089793[1000 rows x 2 columns]>>> df['A']= pd.Series(list(range(len(df))))>>>print(df)B C A00.0898860.511081011.3237661.584758121.489479-0.438671230.831331-0.39802134-0.2482330.49441845-0.0130850.684518560.666951-1.422161671.768838-0.658786782.6610800.648505891.9517510.8362619103.5387851.65747510113.2540342.05260911124.2486201.56840112134.0771730.05562213143.452590-0.20031414152.627620-0.40882915163.690537-0.21044016173.1849240.36544717183.646556-0.15004418194.164563-0.02340519202.3914470.51787220212.8651530.68664921223.6231830.66392722231.5451170.15104423243.5959240.90361924253.0138041.85508325264.4388011.01457226275.1552160.88262827284.4314570.74150928292.8419490.70999129...........970-7.910646-13.738689970971-7.318091-14.811335971972-9.144376-15.466873972973-9.538658-15.367167973974-9.061114-16.822726974975-9.803798-17.368350975976-10.180575-17.270180976977-10.601352-17.671543977978-10.804909-19.535919978979-10.397964-20.361419979980-10.979640-20.300267980981-8.738223-20.202669981982-9.339929-21.528973982983-9.780686-20.902152983984-11.072655-21.235735984985-10.849717-20.439201985986-10.953247-19.708973986987-13.032707-18.687553987988-12.984567-19.557132988989-13.508836-18.747584989990-13.420713-19.883180990991-11.718125-20.474092991992-11.936512-21.360752992993-14.225655-22.006776993994-13.524940-20.844519994995-14.088767-20.492952995996-14.169056-20.666777996997-14.798708-19.960555997998-15.766568-19.395622998999-17.281143-19.089793999[1000 rows x 3 columns]>>> plt.figure()<matplotlib.figure.Figure object at 0x000002A2A0B10F28>>>> df.plot(x='A')<matplotlib.axes._subplots.AxesSubplot object at 0x000002A2A12FE7F0>>>> plt.show()运行结果
运行结果
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了Python数据分析pandas模块用法。分享给大家供大家参考,具体如下:pandaspandas10分钟入门,可以查看官网:10minutest
前言Pandas是Python当中重要的数据分析工具,利用Pandas进行数据分析时,确保使用正确的数据类型是非常重要的,否则可能会导致一些不可预知的错误发生。
pandas对于数据分析的人员来说都是必须熟悉的第三方库,pandas在科学计算上有很大的优势,特别是对于数据分析人员来说,相当的重要。python中有了Num
前言大家在使用Python进行数据分析时,经常要使用到的一个数据结构就是pandas的DataFrame,关于python中pandas.DataFrame的基
pandas模块pandas是一个强大的分析结构化数据的工具集;它的使用基础是Numpy(提供高性能的矩阵运算);用于数据挖掘和数据分析,同时也提供数据清洗功能