Pandas有两种数据结构类型,一个是Series,另一个是DataFrame
Series
Series是一种一维数据结构,类似字典或者Numpy中元素带标签的数组。但是比字典更为强大。其中每一个元素都有一个标签(索引),标签可以是数字或者字符串。具有索引,具有键值对应关系,能够排序,切片Slice等等操作。
DataFrame
DataFrame是一个二维的表结构。Pandas的DataFrame可以存储许多种不同的数据类型,但是每一个列的数据都是同一个数据类型,并且每一个坐标轴都有自己的标签(索引)。你可以把它想象成一个Series的字典项。
1.1 创建Series
利用一个List创建一个Series,Pandas会默认创建整型索引
import pandas as pd
import numpy as np
s =pd.Series([0,1,2,3,4,np.NAN,5,'A'])
In [74]:s
Out[74]:
0 0
1 1
2 2
3 3
4 4
5 NaN
6 5
7 A
dtype: object
2.1 创建DataFrame
方法一:使用一个数组array,指定索引,列名
dates = pd.date_range('20130101',periods=6)
df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=['A','B','C','D'])
In [76]:df
Out[76]:
A B C D
2013-01-01 -2.359309 -0.065001 1.099911 -0.886392
2013-01-02 0.318336 0.715261 0.060752 1.326758
2013-01-03 0.515914 1.482326 -0.973154 1.766126
2013-01-04 1.875221 -0.316619 -0.543997 0.864037
2013-01-05 -0.697887 0.065137 -0.899040 0.826392
2013-01-06 -0.205943 -1.532289 1.849114 1.267895
方法二:使用字典创建DataFrame
df2 = pd.DataFrame({'A':1,
'B':pd.Timestamp('20130102'),
'C':pd.Series(1,index=range(4)),
'D':np.array([3]*4,dtype='int'),
'E':'foo'})
In [78]:df2
Out[78]:
A B C D E
0 1 2013-01-02 1 3 foo
1 1 2013-01-02 1 3 foo
2 1 2013-01-02 1 3 foo
3 1 2013-01-02 1 3 foo
2.1.1 常用的基本功能
1、查看前N行或者后M行数据
In [80]:df.head(2)
Out[80]:
A B C D
2013-01-01 -2.359309 -0.065001 1.099911 -0.886392
2013-01-02 0.318336 0.715261 0.060752 1.326758
In [81]:df.tail(2)
Out[81]:
A B C D
2013-01-05 -0.697887 0.065137 -0.899040 0.826392
2013-01-06 -0.205943 -1.532289 1.849114 1.267895
2、查看索引
In [82]:df.index
Out[82]:
[2013-01-01, ..., 2013-01-06]
Length: 6, Freq: D, Timezone: None
3、查看值
In [83]:df.values
Out[83]:
array([[-2.35930948, -0.06500052, 1.09991148, -0.88639213],
[ 0.31833619, 0.71526129, 0.06075226, 1.32675777],
[ 0.51591397, 1.48232627, -0.97315391, 1.76612637],
[ 1.87522057, -0.31661914, -0.54399686, 0.86403681],
[-0.69788733, 0.06513657, -0.89903951, 0.82639165],
[-0.20594297, -1.53228941, 1.84911405, 1.26789462]])
4、查看列名
In [84]:df.columns
Out[84]: Index([u'A', u'B', u'C', u'D'], dtype='object')In [85]:
In [85]:df.dtypes
Out[85]:
A float64
B float64
C float64
D float64
dtype: object
5、查看数据有多少行
In [74]:len(df)
Out[74]: 6
6、查看数据Summary信息(均值、方差、最小、最大,分位数)
In [9]:df.describe()
Out[9]:
A B C D
count 6.000000 6.000000 6.000000 6.000000
mean 0.329473 0.087595 -0.172075 0.308271
std 0.595492 1.106105 0.524659 0.864240
min -0.218562 -1.454443 -0.992808 -0.790523
25% 0.112395 -0.458519 -0.362685 -0.434517
50% 0.135337 0.000715 -0.197997 0.653177
75% 0.281296 0.630096 0.137386 0.864773
max 1.490029 1.750290 0.524751 1.195568
7、复制一个完全一样的对象
In [11]:df2 = df.copy()
In [11]:df2
Out[11]:
A B C D
2013-01-01 0.134964 -1.454443 -0.310064 1.195568
2013-01-02 1.490029 -0.561749 0.524751 0.522473
2013-01-03 0.329824 1.750290 -0.085930 0.891737
2013-01-04 0.135711 -0.148830 -0.380225 -0.753513
2013-01-05 0.104873 0.150260 0.211825 -0.790523
2013-01-06 -0.218562 0.790041 -0.992808 0.783881
8、对数据进行行列转置
In [12]:df.T
Out[12]:
2013-01-01 2013-01-02 2013-01-03 2013-01-04 2013-01-05 2013-01-06
A 0.134964 1.490029 0.329824 0.135711 0.104873 -0.218562
B -1.454443 -0.561749 1.750290 -0.148830 0.150260 0.790041
C -0.310064 0.524751 -0.085930 -0.380225 0.211825 -0.992808
D 1.195568 0.522473 0.891737 -0.753513 -0.790523 0.783881
9、对数据进行行列转置
In [12]:df.T
Out[12]:
2013-01-01 2013-01-02 2013-01-03 2013-01-04 2013-01-05 2013-01-06
A 0.134964 1.490029 0.329824 0.135711 0.104873 -0.218562
B -1.454443 -0.561749 1.750290 -0.148830 0.150260 0.790041
C -0.310064 0.524751 -0.085930 -0.380225 0.211825 -0.992808
D 1.195568 0.522473 0.891737 -0.753513 -0.790523 0.783881
10、对数据进行行列转置
df.set_index=df['A']
11、对数据进行行列转置
In [74]:df2.columns = ['E','F','G','H']
In [74]:df2
Out[74]:
E F G H
2013-01-01 0.134964 -1.454443 -0.310064 1.195568
2013-01-02 1.490029 -0.561749 0.524751 0.522473
2013-01-03 0.329824 1.750290 -0.085930 0.891737
2013-01-04 0.135711 -0.148830 -0.380225 -0.753513
2013-01-05 0.104873 0.150260 0.211825 -0.790523
2013-01-06 -0.218562 0.790041 -0.992808 0.783881
2.1.2 进行选择、过滤、切片等操作
索引,根据标签(索引)进行行操作
- loc是字符串标签的索引方法,
- iloc是数字标签的索引方法,
- ix是一个字符串标签的索引方法,同样支持数字标签索引作为它的备选。
备注:ix虽然支持字符和数字切片,但有一些轻微的不可预测性,数字标签可能会让ix做出一些奇怪的事情,例如将一个数字解释成一个位置。而loc和iloc则为你带来了安全的、可预测的。ix要比loc和iloc更快。虽然loc是对字符串进行索引,但是如果索引是数字的时候,loc也可以进行索引,貌似有一点矛盾,需要实操时进行体会。
1、选择一列
- 方法一、df['A']
- 方法二、df.A
- 方法三、df.loc[:,['A']]
In [20]:df['A']
Out[20]:
2013-01-01 0.134964
2013-01-02 1.490029
2013-01-03 0.329824
2013-01-04 0.135711
2013-01-05 0.104873
2013-01-06 -0.218562
Freq: D, Name: A, dtype: float64
2、选择两列或者多列
- 方法一、df[['A','B']]
- 方法二、df.loc[:,['A','B']]
- 方法三、df.ix[:,['A','B']]
In [20]:df[['A','B']]
Out[29]:
A B
2013-01-01 0.134964 -1.454443
2013-01-02 1.490029 -0.561749
2013-01-03 0.329824 1.750290
2013-01-04 0.135711 -0.148830
2013-01-05 0.104873 0.150260
2013-01-06 -0.218562 0.790041
3、根据某一列或者几列进行条件筛选
In [30]:df[(df.A>0) & (df.B<0)]
Out[30]:
A B C D
2013-01-01 0.134964 -1.454443 -0.310064 1.195568
2013-01-02 1.490029 -0.561749 0.524751 0.522473
2013-01-04 0.135711 -0.148830 -0.380225 -0.753513
4、索引是数字的使用iloc
In [35]: df1 = pd.DataFrame(np.random.randn(4,4),index=[1,2,3,4],columns=['A','B','C','D'])
In [36]: df1
Out[36]:
A B C D
1 0.913335 -0.209641 -0.994628 -0.300057
2 1.260923 0.405731 -0.566145 -1.114782
3 0.437972 1.800594 -0.269038 -0.038466
4 -0.239472 0.290871 0.207056 0.105834
#查看某一行
In [40]: df.iloc[3]
Out[40]:
A 0.135711
B -0.148830
C -0.380225
D -0.753513
Name: 2013-01-04 00:00:00, dtype: float64
#由于df1的索引是数字,体会一会这里使用loc和iloc的区别
In [25]:df1.loc[1:2]
Out[25]:
A B C D
1 -0.762372 -0.390335 0.037414 2.104834
2 1.265755 -0.113307 1.443822 -2.765101
In [26]:df1.iloc[1:2]
Out[26]:
A B C D
2 1.265755 -0.113307 1.443822 -2.765101
#查看第二行到第三行
In [69]:df.iloc[1:3,:]
Out[69]:
A B C D
2013-01-02 1.490029 -0.561749 0.524751 0.522473
2013-01-03 0.329824 1.750290 -0.085930 0.891737
#查看第一行到第二行,第一列到第三列
In [70]:df.iloc[0:2,0:3]
Out[70]:
A B C
2013-01-01 0.134964 -1.454443 -0.310064
2013-01-02 1.490029 -0.561749 0.524751
#挑某几列进行查看,如位置第1,2,4行,第0,2列
In [71]:df.iloc[[1,2,4],[0,2]]
Out[71]:
A C
2013-01-02 1.490029 0.524751
2013-01-03 0.329824 -0.085930
2013-01-05 0.104873 0.211825
5、索引不是数字,是字符的使用loc
#索引是Date挑 '2013-01-03':'2013-01-05'几行
In [54]:df.loc['2013-01-03':'2013-01-05']
Out[54]:
A B C D
2013-01-03 0.329824 1.75029 -0.085930 0.891737
2013-01-04 0.135711 -0.14883 -0.380225 -0.753513
2013-01-05 0.104873 0.15026 0.211825 -0.790523
In [55]:df.ix['2013-01-03':'2013-01-05']
Out[55]:
A B C D
2013-01-03 0.329824 1.75029 -0.085930 0.891737
2013-01-04 0.135711 -0.14883 -0.380225 -0.753513
2013-01-05 0.104873 0.15026 0.211825 -0.790523
#第1到3列
In [53]:df.iloc[:,1:3]
Out[53]:
B C
2013-01-01 -1.454443 -0.310064
2013-01-02 -0.561749 0.524751
2013-01-03 1.750290 -0.085930
2013-01-04 -0.148830 -0.380225
2013-01-05 0.150260 0.211825
2013-01-06 0.790041 -0.992808
# 第3到5行,A、B列
In [52]:df.loc['2013-01-03':'2013-01-05',['A','B']]
Out[52]:
A B
2013-01-03 0.329824 1.75029
2013-01-04 0.135711 -0.14883
2013-01-05 0.104873 0.15026
In [56]:df.ix[1:2]
Out[56]:
A B C D
2013-01-02 1.490029 -0.561749 0.524751 0.522473
6、排序
#对索引排序
In [57]:df.sort_index(ascending=False)
Out[57]:
A B C D
2013-01-06 -0.218562 0.790041 -0.992808 0.783881
2013-01-05 0.104873 0.150260 0.211825 -0.790523
2013-01-04 0.135711 -0.148830 -0.380225 -0.753513
2013-01-03 0.329824 1.750290 -0.085930 0.891737
2013-01-02 1.490029 -0.561749 0.524751 0.522473
2013-01-01 0.134964 -1.454443 -0.310064 1.195568
#根据某一列进行排序
In [58]:df.sort(columns='B')
Out[58]:
A B C D
2013-01-01 0.134964 -1.454443 -0.310064 1.195568
2013-01-02 1.490029 -0.561749 0.524751 0.522473
2013-01-04 0.135711 -0.148830 -0.380225 -0.753513
2013-01-05 0.104873 0.150260 0.211825 -0.790523
2013-01-06 -0.218562 0.790041 -0.992808 0.783881
2013-01-03 0.329824 1.750290 -0.085930 0.891737
#根据某几列进行排序
In [59]:df.sort(columns=['A','B'])
Out[59]:
A B C D
2013-01-06 -0.218562 0.790041 -0.992808 0.783881
2013-01-05 0.104873 0.150260 0.211825 -0.790523
2013-01-01 0.134964 -1.454443 -0.310064 1.195568
2013-01-04 0.135711 -0.148830 -0.380225 -0.753513
2013-01-03 0.329824 1.750290 -0.085930 0.891737
2013-01-02 1.490029 -0.561749 0.524751 0.522473
7、缺失值处理
In [66]:df3 = df.reindex(index=dates[0:4], columns = list(df.columns)+['E'])
In [66]:df3.loc[dates[0]:dates[1],['E']]=1
In [66]:df3
Out[63]:
A B C D E
2013-01-01 0.134964 -1.454443 -0.310064 1.195568 1
2013-01-02 1.490029 -0.561749 0.524751 0.522473 1
2013-01-03 0.329824 1.750290 -0.085930 0.891737 NaN
2013-01-04 0.135711 -0.148830 -0.380225 -0.753513 NaN
# 删除缺失值
In [60]: df3.dropna(how='any')
Out[60]:
A B C D E
2013-01-01 0.134964 -1.454443 -0.310064 1.195568 1
2013-01-02 1.490029 -0.561749 0.524751 0.522473 1
# 对缺失值进行填充
In [68]:df3.fillna(value=5)
Out[68]:
A B C D E
2013-01-01 0.134964 -1.454443 -0.310064 1.195568 1
2013-01-02 1.490029 -0.561749 0.524751 0.522473 1
2013-01-03 0.329824 1.750290 -0.085930 0.891737 5
2013-01-04 0.135711 -0.148830 -0.380225 -0.753513 5
2.1.3 使用函数求值以及Apply的使用方法
In [69]:df.mean()
Out[69]:
A 0.634212
B -0.517503
C -0.360313
D -0.178633
dtype: float64
In [70]:df.apply(np.cumsum)
Out[70]:
A B C D
2013-01-01 -1.083703 -0.984847 0.231595 0.764466
2013-01-02 -0.277971 -0.737865 -0.366301 -0.768202
2013-01-03 -0.271485 -1.006928 -0.246741 -0.483353
2013-01-04 2.491598 0.096372 -2.159432 -0.331738
2013-01-05 2.624991 -1.882532 -2.445247 -1.636275
2013-01-06 3.805273 -3.105017 -2.161877 -1.071797
In [71]:df.apply(lambda x: x.max() - x.min())
Out[71]:
A 3.846786
B 3.082203
C 2.196061
D 2.297133
dtype: float64