Mike
Mike

Reputation: 385

issue accessing grouped pandas dataframe

I have a dataframe 'test'. I have grouped such dataframe according to a nested groupby rule, which seems to work fine. If I loop through the newly created groups and simply use a print statement:

 print(group)

I get:

<pandas.core.groupby.generic.DataFrameGroupBy object at 0x7fdfb97b0710>

However, if I try:

 group.describe()

I get the following error:

raise ValueError("Empty data passed with indices specified.")
ValueError: Empty data passed with indices specified.

Upvotes: 0

Views: 69

Answers (1)

birdalugur
birdalugur

Reputation: 183

Retrieve a specific group from a group object,I hope this works:

x = group.get_group('name of the group')
x.describe()

Example

>>> df = pd.DataFrame({'X': ['A', 'B', 'A', 'B'], 'Y': [1, 4, 3, 2]})
>>> df
Out[]: 
| index | X | Y |
|-------|---|---|
| 0     | A | 1 |
| 1     | B | 4 |
| 2     | A | 3 |
| 3     | B | 2 |
>>> group = df.groupby(['X'])
>>> x = group.get_group('A')
>>> x.describe()
Out[]:
|       | Y        |
|-------|----------|
| count | 2.000000 |
| mean  | 2.000000 |
| std   | 1.414214 |
| min   | 1.000000 |
| 25%   | 1.500000 |
| 50%   | 2.000000 |
| 75%   | 2.500000 |

Upvotes: 2

Related Questions