Python Numpy arange的区间应按升序显示

1 投票
1 回答
1169 浏览
提问于 2025-04-18 07:56

我用Numpy的'arange'函数创建了一系列的区间:

bins = np.arange(0, df['eCPM'].max(), 0.1)

输出结果看起来是这样的:

[1.8, 1.9)          145940.67         52.569295   1.842306  
[1.9, 2)            150356.59         54.159954   1.932365  
[10.6, 10.7)        150980.84         54.384815  10.626436  
[13.3, 13.4)        152038.63         54.765842  13.373157  
[2, 2.1)            171494.11         61.773901   2.033192  
[2.1, 2.2)          178196.65         64.188223   2.141412  
[2.2, 2.3)          186259.13         67.092410   2.264005 

我想把区间[10.6, 10.7][13.3, 13.4]放到它们应该在的位置,这样所有的区间就能按顺序排列了。

我猜这些区间是以字符串的形式被读取的,所以才会出现这个问题。我尝试添加了一个dtypebins = ..., 0.1, dtype=float),但是没有成功。

[编辑]

import numpy as np
import pandas
df = pandas.read_csv('path/to/file', skip_footer=1)
bins = np.arange(0, df1['eCPM'].max(), 0.1, dtype=float)
df['ecpm group'] = pandas.cut(df['eCPM'], bins, right=False, labels=None)
df =df[['ecpm group', 'Imps', 'Revenue']].groupby('ecpm group').sum()

1 个回答

2

你可以把索引按照“人类的顺序”进行排序,然后再重新编排索引:

import numpy as np
import pandas as pd
import re

def natural_keys(text):
    '''
    alist.sort(key=natural_keys) sorts in human order
    http://nedbatchelder.com/blog/200712/human_sorting.html
    (See Toothy's implementation in the comments)
    '''
    def atoi(text):
        return int(text) if text.isdigit() else text

    return [atoi(c) for c in re.split('(\d+)', text)]

# df = pandas.read_csv('path/to/file', skip_footer=1)
df = pd.DataFrame({'eCPM': np.random.randint(20, size=40)})
bins = np.arange(0, df['eCPM'].max()+1, 0.1, dtype=float)
df['ecpm group'] = pd.cut(df['eCPM'], bins, right=False, labels=None)
df = df.groupby('ecpm group').sum()
df = df.reindex(index=sorted(df.index, key=natural_keys))
print(df)

这样会得到

            eCPM
[0, 0.1)       0
[1, 1.1)       5
[2, 2.1)       4
[4, 4.1)      12
[6, 6.1)      24
[7, 7.1)       7
[8, 8.1)      16
[9, 9.1)      45
[10, 10.1)    40
[11, 11.1)    11
[12, 12.1)    12
[13, 13.1)    13
[15, 15.1)    15
[16, 16.1)    64
[17, 17.1)    34
[18, 18.1)    18

撰写回答