本文共 3620 字,大约阅读时间需要 12 分钟。
pandas是Python中最强大的数据处理库之一,它提供了两种主要的数据对象:Series和DataFrame。这些对象在数据分析和科学计算中具有重要地位。以下将详细介绍这两种数据对象的创建、操作和常用功能。
Series是一种带有索引的序列对象,类似于Python的字典,但其值与索引之间是一一对应的关系。通过以下代码可以创建一个简单的Series对象:
# 创建一个包含数字的Series对象s1 = pd.Series(list("1234"))print(s1)0 11 22 33 4dtype: object 通过传入一个序列给pd.Series初始化一个Series对象,例如列表、元组或生成器。
DataFrame是一种更复杂的数据对象,类似于数据库表,具有行和列的结构。以下是创建DataFrame对象的两种常见方法:
# 通过numpy的二维数组创建DataFrameimport numpy as npdf1 = pd.DataFrame(np.random.randn(6,4))print(df1) 0 1 2 30 NaN 0.0 0.0 0.01 NaN 0.0 0.0 0.02 NaN 0.0 0.0 0.03 NaN 0.0 0.0 0.04 NaN 0.0 0.1 0.25 NaN 0.0 0.3 0.4
# 通过字典创建DataFramedf2 = pd.DataFrame({ 'A' : 1., 'B' : pd.Timestamp('20130102'), 'C' : pd.Series(1, index=list(range(4)), dtype='float32'), 'D' : np.array([3] * 4, dtype='int32'), 'E' : pd.Categorical(["test", "train", "test", "train"]), 'F' : 'foo'})print(df2) A B C D E F0 1.0 2013-01-02 1.0 3 test foo1 1.0 2013-01-02 1.0 3 train foo2 1.0 2013-01-02 1.0 3 test foo3 1.0 2013-01-02 1.0 3 train foo DataFrame对象的索引可以是任何可hashable类型,例如整数、字符串或自定义对象。默认情况下,索引会自动从0开始递增。
无论是Series还是DataFrame对象,都有一个对应的索引。可以通过以下方法查看索引信息:
# 查看Series的索引print(s1.index)Index [0, 1, 2, 3]
为了更好地管理和操作数据,可以对索引进行增删查改操作。
以下是一些常用的数据操作方法:
可以通过以下方式查询DataFrame对象的数据:
# 查看前5行数据print(df.head()) A B C D E F0 1.0 2013-01-02 1.0 3 test foo1 1.0 2013-01-02 1.0 3 train foo2 1.0 2013-01-02 1.0 3 test foo3 1.0 2013-01-02 1.0 3 train foo
通过指定行和列范围可以查看特定范围内的数据:
# 查看第1行print(df.iloc[0])A 1.0B 2013-01-02C 1.0D 3E testF fooName: 0, dtype: object
可以通过条件筛选来获取特定行或列的数据:
# 根据条件筛选数据print(df[df.open > 10].head()) date open close high low volume code378 2017-07-14 10.483 10.570 10.609 10.337 1722570.0 000001379 2017-07-17 10.619 10.483 10.987 10.396 3273123.0 000001380 2017-07-18 10.425 10.716 10.803 10.299 2349431.0 000001381 2017-07-19 10.657 10.754 10.851 10.551 1933075.0 000001382 2017-07-20 10.745 10.638 10.880 10.580 1537338.0 000001
可以通过以下方法对数据进行增删:
# 删除指定列print(df.drop('Open', axis=1).head()) date close high low volume code0 2015-12-24 9.935 10.174 9.871 1039018.0 0000011 2015-12-25 9.879 9.998 9.815 399845.0 0000012 2015-12-28 9.537 9.919 9.537 822408.0 0000013 2015-12-29 9.624 9.632 9.529 619802.0 0000014 2015-12-30 9.632 9.640 9.513 532667.0 000001 可以通过描述统计方法分析数据分布:
# 查看数据描述print(df.describe()) Open mean std min max10.7862 1.5962 8.6580 15.9090Close 9.7927 1.6021 7.6100 14.8600High 9.8942 1.6620 7.7770 14.9980Low 9.6863 1.5424 7.4990 14.4470Volume 833968.6 607731.7 153901.0 4262825.0
可以通过以下方法处理缺失值:
# 删除含有NaN的行print(df.dropna(how='any').head())
可以通过以下方法对数据进行排序:
# 按照Open列排序print(df.sort_values(by='Open').head()) Close Code Date High Low Open Volume10.9190 000001 2015-12-24 9.9980 9.7440 640229.010.8550 000001 2015-12-25 9.9270 9.8150 399845.010.8950 000001 2015-12-28 9.9190 9.5370 822408.010.5450 000001 2015-12-29 9.6320 9.5290 619802.010.6240 000001 2015-12-30 9.6400 9.5130 532667.0
pandas可以与matplotlib结合进行数据可视化:
# 绘制简单的折线图print(df[['Open', 'Close']].plot())
可以通过以下方法将数据读写为文件:
# 将数据保存为CSV文件print(df.to_csv('stock.csv')) 可以通过以下方法读取CSV文件:
# 读取CSV文件并指定列作为索引print(pd.read_csv('stock.csv', index_col=0).head()) 以上只是pandas数据对象的基本操作和常用功能,具体功能和应用场景可以根据需求进一步探索和拓展。
转载地址:http://mvvfk.baihongyu.com/