【问题标题】:Ensuring groupby output type确保 groupby 输出类型
【发布时间】:2016-03-07 09:10:17
【问题描述】:

考虑这个例子:

import pandas as pd
import numpy as np
foo = pd.DataFrame(dict(letter=['a', 'a', 'a', 'b', 'b', 'b', 'a', 'b'],
                 number=[1,1,2,2,3,np.nan, np.nan,4]))
grouped = foo.groupby(foo.number)
print grouped['letter'].transform(lambda x: sum(x=='a'))

Out[18]: 
0    2
1    2
2    1
3    1
4    0
5    b
6    a
7    0

而不是在行上显示1 56'a''b',可能是因为 groupby 在 np.nan 值上被索引。有没有办法阻止这种情况发生,而不用一些虚拟变量替换 nan 值?还有——为什么会这样?

【问题讨论】:

标签: python numpy pandas


【解决方案1】:

pandas 文档在这里解释了这一点:http://pandas.pydata.org/pandas-docs/stable/missing_data.html

NAN 被排除在外,这与 R 一致。

早期版本的 Pandas 确实包含它们,但后来被删除了。

【讨论】:

  • 谢谢。所以,不可能没有虚拟变量。
猜你喜欢
  • 2018-07-17
  • 2020-11-01
  • 2011-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多