列值的范围介于1250、251500等之间

2024-10-02 02:39:15 发布

您现在位置:Python中文网/ 问答频道 /正文

数据集:

    id    MarketPlaceValuation
    0     100
    1     250
    2     200
    3     100
    4     325
    5     175
    6     150
    7     125
    8     225
    9     325
    10    625
    11    100
    12     75
    13    100
    14    200
    15    225
    ..     ..
    40    425
    41    100
    42    275
    43    200
    44    250
    45    500
    46    225
    47    400
    48     75
    49    200

等等。 其中市场估值值介于1-100000之间。你知道吗

样本输出应为:

Valuation Segment
1-250
251-500
501-1000
1001-1500
1501-2000
2001-3000
3001-4000
4001-5000
5001-100000

我试着跟着:

df.groupby(pd.cut(df['MarketPlaceValuation'], np.arange(0,501,250))).count()

但是没有得到正确的输出。你知道吗

接下来,我要根据这个列范围得到其他列的平均值。你知道吗


Tags: 数据iddf市场countnpsegmentpd
1条回答
网友
1楼 · 发布于 2024-10-02 02:39:15

我想你需要:

#specify bins
bins = [0,250,500,1000,1500,2000,3000,4000,5000,100000]
#generate labels from bins
labels = ['{} - {}'.format(i + 1, j) for i, j in zip(bins[:-1], bins[1:])] 

cat = pd.cut(df['MarketPlaceValuation'], bins=bins, labels=labels)
#get mean per categories
df = df.groupby(cat)['MarketPlaceValuation'].mean()

相关问题 更多 >

    热门问题