从pandas系列值创建pandas系列间隔 [英] Create pandas series of intervals from pandas series of values
本文介绍了从pandas系列值创建pandas系列间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我能找到的最接近的答案似乎太复杂了:如何在熊猫中创建间隔列?
The closest answer I can find seems too complex: How I can create an interval column in pandas?
如果我的熊猫数据框看起来像这样:
If I had a pandas data frame that looked like this:
+-------+
| Value |
+-------+
| 6 |
| 12 |
| 56 |
| 60 |
| 120 |
+-------+
我怎么把它变成这个?
+-------+-----------+
| Value | Interval |
+-------+-----------+
| 6 | |
| 12 | (6, 12] |
| 56 | (12, 56] |
| 60 | (56, 60] |
| 120 | (60, 120] |
+-------+-----------+
(注:这是一个非常简化的示例,我的真实数据帧很大,因此性能成为问题.)
(N.B. This is a very simplified example, my real data-frame is big and so performance is an issue.)
推荐答案
pd.cut
defaults to right=True
so if 'Value'
is strictly monotonically increasing
df['Interval'] = pd.cut(df.Value, bins=df.Value)
# Value Interval
#0 6 NaN
#1 12 (6.0, 12.0]
#2 56 (12.0, 56.0]
#3 60 (56.0, 60.0]
#4 120 (60.0, 120.0]
这篇关于从pandas系列值创建pandas系列间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文