[英]Pandas groupby than cut into intervals of the min/max of the group
我有這個數據名:
df = pd.DataFrame({'time' : [1, 1, 1, 1, 1, 2, 2, 2, 2, 2],
'value' : [0.10, 0.25, 0.40, 0.24, 0.20, 0.36, 0.31, 0.20, 0.32, 0.40],
'quantity_A' : [1, 2, 3, 1, 2, 1, 1, 2, 1, 1],
'quantity_B' : [2, 2, 3, 4, 2, 2, 3, 4, 1, 1]})
看起來像這樣:
time value quantity_A quantity_B
0 1 0.10 1 2
1 1 0.25 2 2
2 1 0.40 3 3
3 1 0.24 1 4
4 1 0.20 2 2
5 2 0.36 1 2
6 2 0.31 1 3
7 2 0.20 2 4
8 2 0.32 1 1
9 2 0.40 1 1
我想要這樣的東西:
time interval quantity_A quantity_B
0 1 [0.1, 0.2] 3 4
1 1 (0.2, 0.3] 3 6
2 1 (0.3, 0.4] 3 3
3 2 [0.2, 0.3] 2 4
4 2 (0.3, 0.4] 4 7
或者這將是首選,但它似乎更難做到,因為它不適用於 cut:
time interval quantity_A quantity_B
0 1 0.1 1 2
1 1 0.2 0 0
2 1 0.3 5 8
3 1 0.4 3 3
4 2 0.2 2 4
5 2 0.3 3 6
6 2 0.4 1 1
其中 dataframe 按time
分組, interval
取決於可指定步長的組的min
和max
,在本例中為 0.1。 quantity_A
和quantity_B
應該根據它們所在的組和間隔來求和。我設法通過遍歷整個 dataframe 手動完成此操作,但由於我的數據集很大,因此需要很長時間。 有沒有辦法用 pandas 函數來做到這一點,比如groupby
和cut
以加快速度?
編輯:最小值和最大值應該是每組value
的最小值和最大值。 在這種情況下,時間 == 1 的組有 min = 0.1 和 max = 0.4,對於時間 == 2 的組,min = 0.2 和 max = 0.4 如果第 2 組中有類似 0.54 的值,它將是最大值
不確定是否有可用的內置方法,但是,如果您在間隔上很靈活,那么它是 [0.2, 0.3) 而不是 (0.2, 0.3] 那么以下將起作用:
# one way to truncate the second decimal place
df['value'] = (df['value'] * 10).astype(int) / 10
# rename the column
df.rename(columns={'value': 'interval'}, inplace=True)
# groupby which works same as interval [x ,y) instead of (x, y]
df = df.groupby(['time', 'interval']).sum().reset_index()
Output:
time interval quantity_A quantity_B
0 1 0.1 1 2
1 1 0.2 5 8
2 1 0.4 3 3
3 2 0.2 2 4
4 2 0.3 3 6
5 2 0.4 1 1
每組使用pandas.cut
:
step = 0.1
(df
.groupby('time', group_keys=False)
.apply(lambda g:
g.assign(interval=pd.cut(df['value'],
bins=np.arange(g['value'].min(),
g['value'].max()*1.01,
step),
include_lowest=True)
)
)
.drop(columns='value')
.groupby(['time', 'interval'])
.sum().reset_index()
)
output:
time interval quantity_A quantity_B
0 1 (0.099, 0.2] 3 4
1 1 (0.2, 0.3] 3 6
2 1 (0.3, 0.4] 3 3
3 2 (0.199, 0.3] 2 4
4 2 (0.3, 0.4] 4 7
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.