Python: pandas.cut labels are ignored

好久不见. 提交于 2021-02-10 18:41:03

问题


I want to cut one column in my pandas.DataFrame using pandas.cut(), but the labels I put into labels argument are not applied. Let me show you an example.

I have got the following data frame:

>>> import pandas as pd
>>> df = pd.DataFrame({'x': [-0.009, 0.089, 0.095, 0.096, 0.198]})
>>> print(df)
       x
0 -0.009
1  0.089
2  0.095
3  0.096
4  0.198

And I cut x column like this:

>>> bins = pd.IntervalIndex.from_tuples([(-0.2, -0.1), (-0.1, 0.0), (0.0, 0.1), (0.1, 0.2)])
>>> labels = [100, 200, 300, 400]
>>> df['x_cut'] = pd.cut(df['x'], bins, labels=labels)
>>> print(df)
       x        x_cut
0 -0.009  (-0.1, 0.0]
1  0.089   (0.0, 0.1]
2  0.095   (0.0, 0.1]
3  0.096   (0.0, 0.1]
4  0.198   (0.1, 0.2]

However, I expected the data frame looking like this:

   id      x  x_cut
0   6  0.089    200
1   6  0.089    300
2   6  0.095    300
3   6  0.096    300
4   6  0.098    400

What am I missing? How can I get the data frame with correct labels?


回答1:


It is bug issue 21233.

For me working like @anky_91 commented mapping by dictionary created by zip:

df['x_cut'] = pd.cut(df['x'], bins).map(dict(zip(bins, labels)))
print(df)
       x x_cut
0 -0.009   200
1  0.089   300
2  0.095   300
3  0.096   300
4  0.198   400


来源:https://stackoverflow.com/questions/60846442/python-pandas-cut-labels-are-ignored

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