[英]pandas.cut with enumerated bins
Let us say I have the following data (which is a simplified but accurate representation of my actual data): 假设我有以下数据(这是我的实际数据的简化但准确的表示形式):
df
Age Country
0 10 1
1 15 2
2 20 3
3 25 1
4 30 2
5 15 3
6 20 3
7 15 4
8 20 4
I would like to use pandas.cut to bin countries 1 and 3 into bin1 , and countries 2 and 4 into bin2 . 我想用pandas.cut来斌国家1和3到BIN1,和国家的2和4成BIN2。 Neither binning with a preset number of bins, nor binning with edges will work. 使用预设数量的箱进行装箱或使用边缘进行箱装均无效。 In some possible world, this would be achieved with the following code that just unfortunately happens to be ill-formed in the actual world: 在某些可能的情况下,这将通过以下代码来实现,但不幸的是,这些代码在现实世界中格式不正确:
conts = [‘Africa’, ‘Asia’]
bins = [[1,3], [2,4]]
df['Continent'] = pd.cut(df['Country'], bins, labels = conts)
Is there some functionality in pandas, or a simple workaround I am missing? 熊猫中是否有某些功能,或者我缺少一个简单的解决方法?
The following many-to-one mapping will work for you: 以下多对一映射将为您工作:
dc = {(1,3):'Africa', (2,4):'Asia'}
dc_={}
for keys,v in dc.items():
for k in keys:
dc_[k]=v
df['Continent'] = df['Country'].map(dc_)
df
Age Country Continent
0 10 1 Africa
1 15 2 Asia
2 20 3 Africa
3 25 1 Africa
4 30 2 Asia
5 15 3 Africa
6 20 3 Africa
7 15 4 Asia
8 20 4 Asia
As EdChum already pointed out, map is the way to go here 正如EdChum所指出的,地图是前往此处的方法
continent_lookup = {1: 'Africa', 2: 'Asia', 3: 'Africa', 4: 'Asia'}
df['Continent'] = df.Country.map(continent_lookup)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.