ValueError: Grouper and axis must be same length

旧巷老猫 提交于 2019-12-10 20:05:28

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/49732049/valueerror-grouper-and-axis-must-be-same-length

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!