以下内容为截取自pandas官网的doc(请看这里),我做了一些翻译.
Reshaping by pivoting DataFrame objects
Data is often stored in CSV files or databases in so-called “stacked” or “record” format:
In [1]: df Out[1]: date variable value 0 2000-01-03 A 0.469112 1 2000-01-04 A -0.282863 2 2000-01-05 A -1.509059 3 2000-01-03 B -1.135632 4 2000-01-04 B 1.212112 5 2000-01-05 B -0.173215 6 2000-01-03 C 0.119209 7 2000-01-04 C -1.044236 8 2000-01-05 C -0.861849 9 2000-01-03 D -2.104569 10 2000-01-04 D -0.494929 11 2000-01-05 D 1.071804
以下为一个DataFrame对象的创建过程:
1 import pandas.util.testing as tm; tm.N = 3 2 def unpivot(frame): 3 N, K = frame.shape 4 data = {‘value‘ : frame.values.ravel(‘F‘), 5 ‘variable‘ : np.asarray(frame.columns).repeat(N), 6 ‘date‘ : np.tile(np.asarray(frame.index), K)} 7 return DataFrame(data, columns=[‘date‘, ‘variable‘, ‘value‘]) 8 df = unpivot(tm.makeTimeDataFrame())
我们希望选出variable为A的记录
To select out everything for variable A we could do:
1 In [2]: df[df[‘variable‘] == ‘A‘] 2 Out[2]: 3 date variable value 4 0 2000-01-03 A 0.469112 5 1 2000-01-04 A -0.282863 6 2 2000-01-05 A -1.509059
(以上为非常简单的一个操作,现在我们可以尝试更复杂的操作)
假设我们希望针对variables数据做一些时间序列操作,可以选用这样的表达方式:每一列为一个varibales数据,行的索引为日期,每一个index指示了一组观察数据.使用pivot_table()函数将以上df数据重新整理为符合要求的数据.如下:
1 In [3]: df.pivot(index=‘date‘, columns=‘variable‘, values=‘value‘) 2 Out[3]: 3 variable A B C D 4 date 5 2000-01-03 0.469112 -1.135632 0.119209 -2.104569 6 2000-01-04 -0.282863 1.212112 -1.044236 -0.494929 7 2000-01-05 -1.509059 -0.173215 -0.861849 1.071804
如果在调用pivot_table()时,省略了" values "参数, 而输入的DataFrame对象,除了 被用于index的,以及被用于columns的列以外,还有2个或者更多的列,结果就是一个具有层次列的DataFrame对象,这个对象的最上面的层次分别表示原DataFrame对象的不同的列.
If the values argument is omitted, and the input DataFrame has more than one column of values which are not used as column or index inputs to pivot, then the resulting “pivoted” DataFrame will havehierarchical columns whose topmost level indicates the respective value column:
1 In [4]: df[‘value2‘] = df[‘value‘] * 2 2 3 In [5]: pivoted = df.pivot(‘date‘, ‘variable‘) 4 5 In [6]: pivoted 6 Out[6]: 7 value value2 8 variable A B C D A B 9 date 10 2000-01-03 0.469112 -1.135632 0.119209 -2.104569 0.938225 -2.271265 11 2000-01-04 -0.282863 1.212112 -1.044236 -0.494929 -0.565727 2.424224 12 2000-01-05 -1.509059 -0.173215 -0.861849 1.071804 -3.018117 -0.346429 13 14 15 variable C D 16 date 17 2000-01-03 0.238417 -4.209138 18 2000-01-04 -2.088472 -0.989859 19 2000-01-05 -1.723698 2.143608
以上的DataFrame对象,可以选择特定的subset(子集)
You of course can then select subsets from the pivoted DataFrame:
1 In [7]: pivoted[‘value2‘] 2 Out[7]: 3 variable A B C D 4 date 5 2000-01-03 0.938225 -2.271265 0.238417 -4.209138 6 2000-01-04 -0.565727 2.424224 -2.088472 -0.989859 7 2000-01-05 -3.018117 -0.346429 -1.723698 2.143608
注意,这个取子集的操作,返回的是一个在原DataFrame对象上的一个view(视图),具有相同的数据类型
Note that this returns a view on the underlying data in the case where the data are homogeneously-typed.