user1642513
user1642513

Reputation:

How to apply quantile to pandas groupby object?

I have a pandas groupby object called grouped. I can get grouped.mean() and other simple functions to work, but I cannot get grouped.quantile() to work. I get the following error when attempting to run grouped.quantile():

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

I am grouping by text labels, so I am not sure why the function tries to convert it to a float. It should be computing the quantile using the floats within each group. Can someone help to point out what I am doing wrong?

Upvotes: 4

Views: 12905

Answers (1)

Zelazny7
Zelazny7

Reputation: 40648

It looks like quantile() doesn't ignore the nuisance columns and is trying to find quantiles for your text columns. Here's a trivial example:

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')

However, when I subset out only the numeric columns, I get:

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

Upvotes: 2

Related Questions