如何在熊猫中创建动态垃圾箱

时间:2018-11-06 09:33:48

标签: python pandas

我在熊猫中有以下数据框

   ID   Quantity    
   1    0.45
   2    1.2
   3    3.4
   4    3
   5    23.34
   6    122.34

我想为每个观察结果创建1个垃圾箱

下面是我想要的数据框

  ID   Quantity      buckets  
   1    0.45         0-0.99
   2    1.2          1-1.99 
   3    3.4          3-3.99
   4    3            3-3.99
   5    23.34        23-23.99
   6    122.34       122-122.99

我怎么在熊猫里做?

1 个答案:

答案 0 :(得分:1)

将值转换为整数,然后转换为字符串,最后连接在一起:

s = df['Quantity'].astype(int).astype(str)
df['buckets'] = s + '-' + s + '.99'

替代f-string s:

df['buckets'] = [f'{int(x)}-{int(x)}.99' for x in df['Quantity']]
#https://stackoverflow.com/a/42834054
df['buckets'] = [f'{x:.0f}-{x:.0f}.99' for x in df['Quantity']]

print (df)
   ID  Quantity     buckets
0   1      0.45      0-0.99
1   2      1.20      1-1.99
2   3      3.40      3-3.99
3   4      3.00      3-3.99
4   5     23.34    23-23.99
5   6    122.34  122-122.99

如果需要间隔:

s = df['Quantity'].astype(int)
df['buckets'] = pd.IntervalIndex.from_arrays(s, s + .99)
print (df)
   ID  Quantity          buckets
0   1      0.45      (0.0, 0.99]
1   2      1.20      (1.0, 1.99]
2   3      3.40      (3.0, 3.99]
3   4      3.00      (3.0, 3.99]
4   5     23.34    (23.0, 23.99]
5   6    122.34  (122.0, 122.99]

详细信息:

print (df['Quantity'].astype(int))
0      0
1      1
2      3
3      3
4     23
5    122
Name: Quantity, dtype: int32

print (df['Quantity'].astype(int).astype(str))
0      0
1      1
2      3
3      3
4     23
5    122
Name: Quantity, dtype: object