发布时间:2025-12-10 23:06:17 浏览次数:1
pandas 的安装
pandas 在python上的安装同样的使用pip进行:
pipinstallpandas
pandas 创建对象
pandas 有两种数据结构:Series 和 DataFrame 。
Series
Series 像python中的数据list 一样,每个数据都有自己的索引。从list创建 Series。
>>>importpandasaspd>>>s1=pd.Series([100,23,'bugingcode'])>>>s101001232bugingcodedtype:object>>>
在Series 中添加相应的索引:
>>>importnumpyasnp>>>ts=pd.Series(np.random.randn(365),index=np.arange(1,366))>>>ts
在index中设置索引值是一个从1到366的值。
Series 的数据结构最像的是python中的字典,从字典中创建Series:
sd={'xiaoming':14,'tom':15,'john':13}s4=pd.Series(sd)这时候可以看到Series 已经是自带索引index。
pandas 本身跟 python的另外一个第三方库Matplotlib 有很多的连接,Matplotlib 一个最经常用到的是用来展示数据的,如果还对Matplotlib 不了解的话,后面的章节会进行介绍,现在先拿过来直接用下,如果还没有安装的话,一样的用pip命令安装 pip install Matplotlib , 展示如下数据:
importpandasaspdimportnumpyasnpimportmatplotlib.pyplotaspltts=pd.Series(np.random.randn(365),index=np.arange(1,366))ts.plot()plt.show()
一个不规则的图形,在数据分析中,时间是一个重要的特性,因为很多数据都是跟时间是有关系的,销售额跟时间有关系,天气跟时间有关系。。。,在pandas 中也提供了关于时间的一些函数,使用date_range 生成一系列时间。
>>>pd.date_range('01/01/2017',periods=365)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')>>>之前我们的图形不规则,有一个原因是数据不是连续的,使用cumsum让数据连续:
如下:
importpandasaspdimportnumpyasnpimportmatplotlib.pyplotaspltts=pd.Series(np.random.randn(365),index=pd.date_range('01/01/2017',periods=365))ts=ts.cumsum()ts.plot()plt.show()DataFrame
DataFrame 相当于Series 一维的一个扩展,是一种二维的数据模型,相当于EXcel表格中的数据,有横竖两种坐标,横轴很Series 一样使用index,竖轴用columns 来确定,在建立DataFrame 对象的时候,需要确定三个元素:数据,横轴,竖轴。
df=pd.DataFrame(np.random.randn(8,6),index=pd.date_range('01/01/2018',periods=8),columns=list('ABCDEF'))printdf数据如下:
ABCDEF2018-01-010.7126360.546680-0.847866-0.6290052.1526860.5639072018-01-02-1.2927991.1220980.7432930.6564120.9897382.4682002018-01-031.7628940.783614-0.3014680.289608-0.7808440.8730742018-01-04-0.8180661.629542-0.5954510.9101410.1609800.3066602018-01-052.0086580.456592-0.8395971.6150130.718422-0.5645842018-01-060.4808930.724015-1.076434-0.2537310.337147-0.0282122018-01-07-0.6725010.739550-1.3160941.118234-1.456680-0.6018902018-01-08-1.028436-1.036542-0.4590441.321962-0.198338-1.034822
在数据分析的过程中,很常见的一种情况是数据直接从excel 或者cvs 过来,可以excel中读取数据到DataFrame ,数据在 DataFrame 中进行处理:
df=pd.read_excel('data.xlsx',sheet_name='Sheet1')printdf同样的有保存数据到excel 中 to_excel。
处理cvs数据的函数是:read_cvs 和 to_cvs ,处理HDF5的函数为 read_hdf 和 to_hdf 。
访问DataFrame 可以跟二位数组一样的访问方式:
printdf['A']
带出横轴标签:
2018-01-010.7126362018-01-02-1.2927992018-01-031.7628942018-01-04-0.8180662018-01-052.0086582018-01-060.4808932018-01-07-0.6725012018-01-08-1.028436
同样的可以指定某一个元素:
printdf['A']['2018-01-01']
对数组进行切片出来,认清横轴和纵轴:
>>>importpandasaspd>>>df=pd.read_excel('data.xlsx',sheet_name='Sheet1')>>>df[:][0:3]ABCDEF2018-01-010.7126360.546680-0.847866-0.6290052.1526860.5639072018-01-02-1.2927991.1220980.7432930.6564120.9897382.4682002018-01-031.7628940.783614-0.3014680.289608-0.7808440.873074>>>看完上述内容,你们掌握pandas库怎么在python中进行安装的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注本站行业资讯频道,感谢各位的阅读!