user3194861
user3194861

Reputation: 314

ValueError: Grouper and axis must be same length

I have a dataframe with 38 columns, one of them is Time. I established a bin frame space

timeframe=['4-6','7-9','10-12','13-15','16-18','19-21','22-24' ]
bins = [3,6,9,12,15,18,21,24]

Now I cut:

frameddata=pd.cut(df['time'],bins,retbins=True, labels=timeframe)

and want to group the df for different bins:

groups=df.groupby(frameddata)

here I get the following error:

ValueError: Grouper and axis must be same length

Any help on this?

Upvotes: 4

Views: 4574

Answers (1)

jezrael
jezrael

Reputation: 863741

I believe need create new column:

df['bins'] = pd.cut(df['time'],bins,retbins=True, labels=timeframe)
groups=df.groupby('bins')

But is possible you get some NaNs in new column, because values outside of range 4-24, so groupby silently remove these rows.

Upvotes: 2

Related Questions