前面的章节关注于不同类型的数据规整流程和NumPy、pandas与其它库的特点。随着时间的发展,pandas发展出了更多适合高级用户的功能。本章就要深入学习pandas的高级功能。

12.1 分类数据

这一节介绍的是pandas的分类类型。我会向你展示通过使用它,提高性能和内存的使用率。我还会介绍一些在统计和机器学习中使用分类数据的工具。

背景和目的

表中的一列通常会有重复的包含不同值的小集合的情况。我们已经学过了unique和value_counts,它们可以从数组提取出不同的值,并分别计算频率:

  1. In [10]: import numpy as np; import pandas as pd
  2. In [11]: values = pd.Series(['apple', 'orange', 'apple',
  3. ....: 'apple'] * 2)
  4. In [12]: values
  5. Out[12]:
  6. 0 apple
  7. 1 orange
  8. 2 apple
  9. 3 apple
  10. 4 apple
  11. 5 orange
  12. 6 apple
  13. 7 apple
  14. dtype: object
  15. In [13]: pd.unique(values)
  16. Out[13]: array(['apple', 'orange'], dtype=object)
  17. In [14]: pd.value_counts(values)
  18. Out[14]:
  19. apple 6
  20. orange 2
  21. dtype: int64

许多数据系统(数据仓库、统计计算或其它应用)都发展出了特定的表征重复值的方法,以进行高效的存储和计算。在数据仓库中,最好的方法是使用所谓的包含不同值的维表(Dimension Table),将主要的参数存储为引用维表整数键:

  1. In [15]: values = pd.Series([0, 1, 0, 0] * 2)
  2. In [16]: dim = pd.Series(['apple', 'orange'])
  3. In [17]: values
  4. Out[17]:
  5. 0 0
  6. 1 1
  7. 2 0
  8. 3 0
  9. 4 0
  10. 5 1
  11. 6 0
  12. 7 0
  13. dtype: int64
  14. In [18]: dim
  15. Out[18]:
  16. 0 apple
  17. 1 orange
  18. dtype: object

可以使用take方法存储原始的字符串Series:

  1. In [19]: dim.take(values)
  2. Out[19]:
  3. 0 apple
  4. 1 orange
  5. 0 apple
  6. 0 apple
  7. 0 apple
  8. 1 orange
  9. 0 apple
  10. 0 apple
  11. dtype: object

这种用整数表示的方法称为分类或字典编码表示法。不同值得数组称为分类、字典或数据级。本书中,我们使用分类的说法。表示分类的整数值称为分类编码或简单地称为编码。

分类表示可以在进行分析时大大的提高性能。你也可以在保持编码不变的情况下,对分类进行转换。一些相对简单的转变例子包括:

  • 重命名分类。
  • 加入一个新的分类,不改变已经存在的分类的顺序或位置。

pandas的分类类型

pandas有一个特殊的分类类型,用于保存使用整数分类表示法的数据。看一个之前的Series例子:

  1. In [20]: fruits = ['apple', 'orange', 'apple', 'apple'] * 2
  2. In [21]: N = len(fruits)
  3. In [22]: df = pd.DataFrame({'fruit': fruits,
  4. ....: 'basket_id': np.arange(N),
  5. ....: 'count': np.random.randint(3, 15, size=N),
  6. ....: 'weight': np.random.uniform(0, 4, size=N)},
  7. ....: columns=['basket_id', 'fruit', 'count', 'weight'])
  8. In [23]: df
  9. Out[23]:
  10. basket_id fruit count weight
  11. 0 0 apple 5 3.858058
  12. 1 1 orange 8 2.612708
  13. 2 2 apple 4 2.995627
  14. 3 3 apple 7 2.614279
  15. 4 4 apple 12 2.990859
  16. 5 5 orange 8 3.845227
  17. 6 6 apple 5 0.033553
  18. 7 7 apple 4 0.425778

这里,df[‘fruit’]是一个Python字符串对象的数组。我们可以通过调用它,将它转变为分类:

  1. In [24]: fruit_cat = df['fruit'].astype('category')
  2. In [25]: fruit_cat
  3. Out[25]:
  4. 0 apple
  5. 1 orange
  6. 2 apple
  7. 3 apple
  8. 4 apple
  9. 5 orange
  10. 6 apple
  11. 7 apple
  12. Name: fruit, dtype: category
  13. Categories (2, object): [apple, orange]

fruit_cat的值不是NumPy数组,而是一个pandas.Categorical实例:

  1. In [26]: c = fruit_cat.values
  2. In [27]: type(c)
  3. Out[27]: pandas.core.categorical.Categorical

分类对象有categories和codes属性:

  1. In [28]: c.categories
  2. Out[28]: Index(['apple', 'orange'], dtype='object')
  3. In [29]: c.codes
  4. Out[29]: array([0, 1, 0, 0, 0, 1, 0, 0], dtype=int8)

你可将DataFrame的列通过分配转换结果,转换为分类:

  1. In [30]: df['fruit'] = df['fruit'].astype('category')
  2. In [31]: df.fruit
  3. Out[31]:
  4. 0 apple
  5. 1 orange
  6. 2 apple
  7. 3 apple
  8. 4 apple
  9. 5 orange
  10. 6 apple
  11. 7 apple
  12. Name: fruit, dtype: category
  13. Categories (2, object): [apple, orange]

你还可以从其它Python序列直接创建pandas.Categorical:

  1. In [32]: my_categories = pd.Categorical(['foo', 'bar', 'baz', 'foo', 'bar'])
  2. In [33]: my_categories
  3. Out[33]:
  4. [foo, bar, baz, foo, bar]
  5. Categories (3, object): [bar, baz, foo]

如果你已经从其它源获得了分类编码,你还可以使用from_codes构造器:

  1. In [34]: categories = ['foo', 'bar', 'baz']
  2. In [35]: codes = [0, 1, 2, 0, 0, 1]
  3. In [36]: my_cats_2 = pd.Categorical.from_codes(codes, categories)
  4. In [37]: my_cats_2
  5. Out[37]:
  6. [foo, bar, baz, foo, foo, bar]
  7. Categories (3, object): [foo, bar, baz]

与显示指定不同,分类变换不认定指定的分类顺序。因此取决于输入数据的顺序,categories数组的顺序会不同。当使用from_codes或其它的构造器时,你可以指定分类一个有意义的顺序:

  1. In [38]: ordered_cat = pd.Categorical.from_codes(codes, categories,
  2. ....: ordered=True)
  3. In [39]: ordered_cat
  4. Out[39]:
  5. [foo, bar, baz, foo, foo, bar]
  6. Categories (3, object): [foo < bar < baz]

输出[foo < bar < baz]指明‘foo’位于‘bar’的前面,以此类推。无序的分类实例可以通过as_ordered排序:

  1. In [40]: my_cats_2.as_ordered()
  2. Out[40]:
  3. [foo, bar, baz, foo, foo, bar]
  4. Categories (3, object): [foo < bar < baz]

最后要注意,分类数据不需要字符串,尽管我仅仅展示了字符串的例子。分类数组可以包括任意不可变类型。

用分类进行计算

与非编码版本(比如字符串数组)相比,使用pandas的Categorical有些类似。某些pandas组件,比如groupby函数,更适合进行分类。还有一些函数可以使用有序标志位。

来看一些随机的数值数据,使用pandas.qcut面元函数。它会返回pandas.Categorical,我们之前使用过pandas.cut,但没解释分类是如何工作的:

  1. In [41]: np.random.seed(12345)
  2. In [42]: draws = np.random.randn(1000)
  3. In [43]: draws[:5]
  4. Out[43]: array([-0.2047, 0.4789, -0.5194, -0.5557, 1.9658])

计算这个数据的分位面元,提取一些统计信息:

  1. In [44]: bins = pd.qcut(draws, 4)
  2. In [45]: bins
  3. Out[45]:
  4. [(-0.684, -0.0101], (-0.0101, 0.63], (-0.684, -0.0101], (-0.684, -0.0101], (0.63,
  5. 3.928], ..., (-0.0101, 0.63], (-0.684, -0.0101], (-2.95, -0.684], (-0.0101, 0.63
  6. ], (0.63, 3.928]]
  7. Length: 1000
  8. Categories (4, interval[float64]): [(-2.95, -0.684] < (-0.684, -0.0101] < (-0.010
  9. 1, 0.63] <
  10. (0.63, 3.928]]

虽然有用,确切的样本分位数与分位的名称相比,不利于生成汇总。我们可以使用labels参数qcut,实现目的:

  1. In [46]: bins = pd.qcut(draws, 4, labels=['Q1', 'Q2', 'Q3', 'Q4'])
  2. In [47]: bins
  3. Out[47]:
  4. [Q2, Q3, Q2, Q2, Q4, ..., Q3, Q2, Q1, Q3, Q4]
  5. Length: 1000
  6. Categories (4, object): [Q1 < Q2 < Q3 < Q4]
  7. In [48]: bins.codes[:10]
  8. Out[48]: array([1, 2, 1, 1, 3, 3, 2, 2, 3, 3], dtype=int8)

加上标签的面元分类不包含数据面元边界的信息,因此可以使用groupby提取一些汇总信息:

  1. In [49]: bins = pd.Series(bins, name='quartile')
  2. In [50]: results = (pd.Series(draws)
  3. ....: .groupby(bins)
  4. ....: .agg(['count', 'min', 'max'])
  5. ....: .reset_index())
  6. In [51]: results
  7. Out[51]:
  8. quartile count min max
  9. 0 Q1 250 -2.949343 -0.685484
  10. 1 Q2 250 -0.683066 -0.010115
  11. 2 Q3 250 -0.010032 0.628894
  12. 3 Q4 250 0.634238 3.927528

分位数列保存了原始的面元分类信息,包括排序:

  1. In [52]: results['quartile']
  2. Out[52]:
  3. 0 Q1
  4. 1 Q2
  5. 2 Q3
  6. 3 Q4
  7. Name: quartile, dtype: category
  8. Categories (4, object): [Q1 < Q2 < Q3 < Q4]

用分类提高性能

如果你是在一个特定数据集上做大量分析,将其转换为分类可以极大地提高效率。DataFrame列的分类使用的内存通常少的多。来看一些包含一千万元素的Series,和一些不同的分类:

  1. In [53]: N = 10000000
  2. In [54]: draws = pd.Series(np.random.randn(N))
  3. In [55]: labels = pd.Series(['foo', 'bar', 'baz', 'qux'] * (N // 4))

现在,将标签转换为分类:

  1. In [56]: categories = labels.astype('category')

这时,可以看到标签使用的内存远比分类多:

  1. In [57]: labels.memory_usage()
  2. Out[57]: 80000080
  3. In [58]: categories.memory_usage()
  4. Out[58]: 10000272

转换为分类不是没有代价的,但这是一次性的代价:

  1. In [59]: %time _ = labels.astype('category')
  2. CPU times: user 490 ms, sys: 240 ms, total: 730 ms
  3. Wall time: 726 ms

GroupBy使用分类操作明显更快,是因为底层的算法使用整数编码数组,而不是字符串数组。

分类方法

包含分类数据的Series有一些特殊的方法,类似于Series.str字符串方法。它还提供了方便的分类和编码的使用方法。看下面的Series:

  1. In [60]: s = pd.Series(['a', 'b', 'c', 'd'] * 2)
  2. In [61]: cat_s = s.astype('category')
  3. In [62]: cat_s
  4. Out[62]:
  5. 0 a
  6. 1 b
  7. 2 c
  8. 3 d
  9. 4 a
  10. 5 b
  11. 6 c
  12. 7 d
  13. dtype: category
  14. Categories (4, object): [a, b, c, d]

特别的cat属性提供了分类方法的入口:

  1. In [63]: cat_s.cat.codes
  2. Out[63]:
  3. 0 0
  4. 1 1
  5. 2 2
  6. 3 3
  7. 4 0
  8. 5 1
  9. 6 2
  10. 7 3
  11. dtype: int8
  12. In [64]: cat_s.cat.categories
  13. Out[64]: Index(['a', 'b', 'c', 'd'], dtype='object')

假设我们知道这个数据的实际分类集,超出了数据中的四个值。我们可以使用set_categories方法改变它们:

  1. In [65]: actual_categories = ['a', 'b', 'c', 'd', 'e']
  2. In [66]: cat_s2 = cat_s.cat.set_categories(actual_categories)
  3. In [67]: cat_s2
  4. Out[67]:
  5. 0 a
  6. 1 b
  7. 2 c
  8. 3 d
  9. 4 a
  10. 5 b
  11. 6 c
  12. 7 d
  13. dtype: category
  14. Categories (5, object): [a, b, c, d, e]

虽然数据看起来没变,新的分类将反映在它们的操作中。例如,如果有的话,value_counts表示分类:

  1. In [68]: cat_s.value_counts()
  2. Out[68]:
  3. d 2
  4. c 2
  5. b 2
  6. a 2
  7. dtype: int64
  8. In [69]: cat_s2.value_counts()
  9. Out[69]:
  10. d 2
  11. c 2
  12. b 2
  13. a 2
  14. e 0
  15. dtype: int64

在大数据集中,分类经常作为节省内存和高性能的便捷工具。过滤完大DataFrame或Series之后,许多分类可能不会出现在数据中。我们可以使用remove_unused_categories方法删除没看到的分类:

  1. In [70]: cat_s3 = cat_s[cat_s.isin(['a', 'b'])]
  2. In [71]: cat_s3
  3. Out[71]:
  4. 0 a
  5. 1 b
  6. 4 a
  7. 5 b
  8. dtype: category
  9. Categories (4, object): [a, b, c, d]
  10. In [72]: cat_s3.cat.remove_unused_categories()
  11. Out[72]:
  12. 0 a
  13. 1 b
  14. 4 a
  15. 5 b
  16. dtype: category
  17. Categories (2, object): [a, b]

表12-1列出了可用的分类方法。

第12章 pandas高级应用 - 图1

为建模创建虚拟变量

当你使用统计或机器学习工具时,通常会将分类数据转换为虚拟变量,也称为one-hot编码。这包括创建一个不同类别的列的DataFrame;这些列包含给定分类的1s,其它为0。

看前面的例子:

  1. In [73]: cat_s = pd.Series(['a', 'b', 'c', 'd'] * 2, dtype='category')

前面的第7章提到过,pandas.get_dummies函数可以转换这个分类数据为包含虚拟变量的DataFrame:

  1. In [74]: pd.get_dummies(cat_s)
  2. Out[74]:
  3. a b c d
  4. 0 1 0 0 0
  5. 1 0 1 0 0
  6. 2 0 0 1 0
  7. 3 0 0 0 1
  8. 4 1 0 0 0
  9. 5 0 1 0 0
  10. 6 0 0 1 0
  11. 7 0 0 0 1

12.2 GroupBy高级应用

尽管我们在第10章已经深度学习了Series和DataFrame的Groupby方法,还有一些方法也是很有用的。

分组转换和“解封”GroupBy

在第10章,我们在分组操作中学习了apply方法,进行转换。还有另一个transform方法,它与apply很像,但是对使用的函数有一定限制:

  • 它可以产生向分组形状广播标量值
  • 它可以产生一个和输入组形状相同的对象
  • 它不能修改输入

来看一个简单的例子:

  1. In [75]: df = pd.DataFrame({'key': ['a', 'b', 'c'] * 4,
  2. ....: 'value': np.arange(12.)})
  3. In [76]: df
  4. Out[76]:
  5. key value
  6. 0 a 0.0
  7. 1 b 1.0
  8. 2 c 2.0
  9. 3 a 3.0
  10. 4 b 4.0
  11. 5 c 5.0
  12. 6 a 6.0
  13. 7 b 7.0
  14. 8 c 8.0
  15. 9 a 9.0
  16. 10 b 10.0
  17. 11 c 11.0

按键进行分组:

  1. In [77]: g = df.groupby('key').value
  2. In [78]: g.mean()
  3. Out[78]:
  4. key
  5. a 4.5
  6. b 5.5
  7. c 6.5
  8. Name: value, dtype: float64

假设我们想产生一个和df[‘value’]形状相同的Series,但值替换为按键分组的平均值。我们可以传递函数lambda x: x.mean()进行转换:

  1. In [79]: g.transform(lambda x: x.mean())
  2. Out[79]:
  3. 0 4.5
  4. 1 5.5
  5. 2 6.5
  6. 3 4.5
  7. 4 5.5
  8. 5 6.5
  9. 6 4.5
  10. 7 5.5
  11. 8 6.5
  12. 9 4.5
  13. 10 5.5
  14. 11 6.5
  15. Name: value, dtype: float64

对于内置的聚合函数,我们可以传递一个字符串假名作为GroupBy的agg方法:

  1. In [80]: g.transform('mean')
  2. Out[80]:
  3. 0 4.5
  4. 1 5.5
  5. 2 6.5
  6. 3 4.5
  7. 4 5.5
  8. 5 6.5
  9. 6 4.5
  10. 7 5.5
  11. 8 6.5
  12. 9 4.5
  13. 10 5.5
  14. 11 6.5
  15. Name: value, dtype: float64

与apply类似,transform的函数会返回Series,但是结果必须与输入大小相同。举个例子,我们可以用lambda函数将每个分组乘以2:

  1. In [81]: g.transform(lambda x: x * 2)
  2. Out[81]:
  3. 0 0.0
  4. 1 2.0
  5. 2 4.0
  6. 3 6.0
  7. 4 8.0
  8. 5 10.0
  9. 6 12.0
  10. 7 14.0
  11. 8 16.0
  12. 9 18.0
  13. 10 20.0
  14. 11 22.0
  15. Name: value, dtype: float64

再举一个复杂的例子,我们可以计算每个分组的降序排名:

  1. In [82]: g.transform(lambda x: x.rank(ascending=False))
  2. Out[82]:
  3. 0 4.0
  4. 1 4.0
  5. 2 4.0
  6. 3 3.0
  7. 4 3.0
  8. 5 3.0
  9. 6 2.0
  10. 7 2.0
  11. 8 2.0
  12. 9 1.0
  13. 10 1.0
  14. 11 1.0
  15. Name: value, dtype: float64

看一个由简单聚合构造的的分组转换函数:

  1. def normalize(x):
  2. return (x - x.mean()) / x.std()

我们用transform或apply可以获得等价的结果:

  1. In [84]: g.transform(normalize)
  2. Out[84]:
  3. 0 -1.161895
  4. 1 -1.161895
  5. 2 -1.161895
  6. 3 -0.387298
  7. 4 -0.387298
  8. 5 -0.387298
  9. 6 0.387298
  10. 7 0.387298
  11. 8 0.387298
  12. 9 1.161895
  13. 10 1.161895
  14. 11 1.161895
  15. Name: value, dtype: float64
  16. In [85]: g.apply(normalize)
  17. Out[85]:
  18. 0 -1.161895
  19. 1 -1.161895
  20. 2 -1.161895
  21. 3 -0.387298
  22. 4 -0.387298
  23. 5 -0.387298
  24. 6 0.387298
  25. 7 0.387298
  26. 8 0.387298
  27. 9 1.161895
  28. 10 1.161895
  29. 11 1.161895
  30. Name: value, dtype: float64

内置的聚合函数,比如mean或sum,通常比apply函数快,也比transform快。这允许我们进行一个所谓的解封(unwrapped)分组操作:

  1. In [86]: g.transform('mean')
  2. Out[86]:
  3. 0 4.5
  4. 1 5.5
  5. 2 6.5
  6. 3 4.5
  7. 4 5.5
  8. 5 6.5
  9. 6 4.5
  10. 7 5.5
  11. 8 6.5
  12. 9 4.5
  13. 10 5.5
  14. 11 6.5
  15. Name: value, dtype: float64
  16. In [87]: normalized = (df['value'] - g.transform('mean')) / g.transform('std')
  17. In [88]: normalized
  18. Out[88]:
  19. 0 -1.161895
  20. 1 -1.161895
  21. 2 -1.161895
  22. 3 -0.387298
  23. 4 -0.387298
  24. 5 -0.387298
  25. 6 0.387298
  26. 7 0.387298
  27. 8 0.387298
  28. 9 1.161895
  29. 10 1.161895
  30. 11 1.161895
  31. Name: value, dtype: float64

解封分组操作可能包括多个分组聚合,但是矢量化操作还是会带来收益。

分组的时间重采样

对于时间序列数据,resample方法从语义上是一个基于内在时间的分组操作。下面是一个示例表:

  1. In [89]: N = 15
  2. In [90]: times = pd.date_range('2017-05-20 00:00', freq='1min', periods=N)
  3. In [91]: df = pd.DataFrame({'time': times,
  4. ....: 'value': np.arange(N)})
  5. In [92]: df
  6. Out[92]:
  7. time value
  8. 0 2017-05-20 00:00:00 0
  9. 1 2017-05-20 00:01:00 1
  10. 2 2017-05-20 00:02:00 2
  11. 3 2017-05-20 00:03:00 3
  12. 4 2017-05-20 00:04:00 4
  13. 5 2017-05-20 00:05:00 5
  14. 6 2017-05-20 00:06:00 6
  15. 7 2017-05-20 00:07:00 7
  16. 8 2017-05-20 00:08:00 8
  17. 9 2017-05-20 00:09:00 9
  18. 10 2017-05-20 00:10:00 10
  19. 11 2017-05-20 00:11:00 11
  20. 12 2017-05-20 00:12:00 12
  21. 13 2017-05-20 00:13:00 13
  22. 14 2017-05-20 00:14:00 14

这里,我们可以用time作为索引,然后重采样:

  1. In [93]: df.set_index('time').resample('5min').count()
  2. Out[93]:
  3. value
  4. time
  5. 2017-05-20 00:00:00 5
  6. 2017-05-20 00:05:00 5
  7. 2017-05-20 00:10:00 5

假设DataFrame包含多个时间序列,用一个额外的分组键的列进行标记:

  1. In [94]: df2 = pd.DataFrame({'time': times.repeat(3),
  2. ....: 'key': np.tile(['a', 'b', 'c'], N),
  3. ....: 'value': np.arange(N * 3.)})
  4. In [95]: df2[:7]
  5. Out[95]:
  6. key time value
  7. 0 a 2017-05-20 00:00:00 0.0
  8. 1 b 2017-05-20 00:00:00 1.0
  9. 2 c 2017-05-20 00:00:00 2.0
  10. 3 a 2017-05-20 00:01:00 3.0
  11. 4 b 2017-05-20 00:01:00 4.0
  12. 5 c 2017-05-20 00:01:00 5.0
  13. 6 a 2017-05-20 00:02:00 6.0

要对每个key值进行相同的重采样,我们引入pandas.TimeGrouper对象:

  1. In [96]: time_key = pd.TimeGrouper('5min')

我们然后设定时间索引,用key和time_key分组,然后聚合:

  1. In [97]: resampled = (df2.set_index('time')
  2. ....: .groupby(['key', time_key])
  3. ....: .sum())
  4. In [98]: resampled
  5. Out[98]:
  6. value
  7. key time
  8. a 2017-05-20 00:00:00 30.0
  9. 2017-05-20 00:05:00 105.0
  10. 2017-05-20 00:10:00 180.0
  11. b 2017-05-20 00:00:00 35.0
  12. 2017-05-20 00:05:00 110.0
  13. 2017-05-20 00:10:00 185.0
  14. c 2017-05-20 00:00:00 40.0
  15. 2017-05-20 00:05:00 115.0
  16. 2017-05-20 00:10:00 190.0
  17. In [99]: resampled.reset_index()
  18. Out[99]:
  19. key time value
  20. 0 a 2017-05-20 00:00:00 30.0
  21. 1 a 2017-05-20 00:05:00 105.0
  22. 2 a 2017-05-20 00:10:00 180.0
  23. 3 b 2017-05-20 00:00:00 35.0
  24. 4 b 2017-05-20 00:05:00 110.0
  25. 5 b 2017-05-20 00:10:00 185.0
  26. 6 c 2017-05-20 00:00:00 40.0
  27. 7 c 2017-05-20 00:05:00 115.0
  28. 8 c 2017-05-20 00:10:00 190.0

使用TimeGrouper的限制是时间必须是Series或DataFrame的索引。

12.3 链式编程技术

当对数据集进行一系列变换时,你可能发现创建的多个临时变量其实并没有在分析中用到。看下面的例子:

  1. df = load_data()
  2. df2 = df[df['col2'] < 0]
  3. df2['col1_demeaned'] = df2['col1'] - df2['col1'].mean()
  4. result = df2.groupby('key').col1_demeaned.std()

虽然这里没有使用真实的数据,这个例子却指出了一些新方法。首先,DataFrame.assign方法是一个df[k] = v形式的函数式的列分配方法。它不是就地修改对象,而是返回新的修改过的DataFrame。因此,下面的语句是等价的:

  1. # Usual non-functional way
  2. df2 = df.copy()
  3. df2['k'] = v
  4. # Functional assign way
  5. df2 = df.assign(k=v)

就地分配可能会比assign快,但是assign可以方便地进行链式编程:

  1. result = (df2.assign(col1_demeaned=df2.col1 - df2.col2.mean())
  2. .groupby('key')
  3. .col1_demeaned.std())

我使用外括号,这样便于添加换行符。

使用链式编程时要注意,你可能会需要涉及临时对象。在前面的例子中,我们不能使用load_data的结果,直到它被赋值给临时变量df。为了这么做,assign和许多其它pandas函数可以接收类似函数的参数,即可调用对象(callable)。为了展示可调用对象,看一个前面例子的片段:

  1. df = load_data()
  2. df2 = df[df['col2'] < 0]

它可以重写为:

  1. df = (load_data()
  2. [lambda x: x['col2'] < 0])

这里,load_data的结果没有赋值给某个变量,因此传递到[ ]的函数在这一步被绑定到了对象。

我们可以把整个过程写为一个单链表达式:

  1. result = (load_data()
  2. [lambda x: x.col2 < 0]
  3. .assign(col1_demeaned=lambda x: x.col1 - x.col1.mean())
  4. .groupby('key')
  5. .col1_demeaned.std())

是否将代码写成这种形式只是习惯而已,将它分开成若干步可以提高可读性。

管道方法

你可以用Python内置的pandas函数和方法,用带有可调用对象的链式编程做许多工作。但是,有时你需要使用自己的函数,或是第三方库的函数。这时就要用到管道方法。

看下面的函数调用:

  1. a = f(df, arg1=v1)
  2. b = g(a, v2, arg3=v3)
  3. c = h(b, arg4=v4)

当使用接收、返回Series或DataFrame对象的函数式,你可以调用pipe将其重写:

  1. result = (df.pipe(f, arg1=v1)
  2. .pipe(g, v2, arg3=v3)
  3. .pipe(h, arg4=v4))

f(df)和df.pipe(f)是等价的,但是pipe使得链式声明更容易。

pipe的另一个有用的地方是提炼操作为可复用的函数。看一个从列减去分组方法的例子:

  1. g = df.groupby(['key1', 'key2'])
  2. df['col1'] = df['col1'] - g.transform('mean')

假设你想转换多列,并修改分组的键。另外,你想用链式编程做这个转换。下面就是一个方法:

  1. def group_demean(df, by, cols):
  2. result = df.copy()
  3. g = df.groupby(by)
  4. for c in cols:
  5. result[c] = df[c] - g[c].transform('mean')
  6. return result

然后可以写为:

  1. result = (df[df.col1 < 0]
  2. .pipe(group_demean, ['key1', 'key2'], ['col1']))

12.4 总结

和其它许多开源项目一样,pandas仍然在不断的变化和进步中。和本书中其它地方一样,这里的重点是放在接下来几年不会发生什么改变且稳定的功能。

为了深入学习pandas的知识,我建议你学习官方文档,并阅读开发团队发布的文档更新。我们还邀请你加入pandas的开发工作:修改bug、创建新功能、完善文档。