如何将分位数应用于pandas groupby对象?

时间:2013-01-02 16:20:20

标签: python pandas

我有一个名为grouped的pandas groupby对象。我可以使grouped.mean()和其他简单函数起作用,但我无法使grouped.quantile()起作用。尝试运行grouped.quantile()时出现以下错误:

ValueError: ('invalid literal for float(): groupA', u'occurred at index groups')

我按文字标签分组,所以我不确定为什么函数会尝试将其转换为float。它应该使用每组中的浮点数来计算分位数。有人可以帮助指出我做错了吗?

1 个答案:

答案 0 :(得分:2)

看起来,分位数()不会忽略讨厌的列,并且正在尝试查找文本列的分位数。这是一个简单的例子:

In [75]: df = DataFrame({'col1':['A','A','B','B'], 'col2':[1,2,3,4]})

In [76]: df
Out[76]:
  col1  col2
0    A     1
1    A     2
2    B     3
3    B     4

In [77]: df.groupby('col1').quantile()
ValueError: ('could not convert string to float: A', u'occurred at index col1')

但是,当我只对数字列进行子集时,我得到:

In [78]: df.groupby('col1')['col2'].quantile()
Out[78]:
col1
A       1.5
B       3.5
相关问题