是否有一种方法可以将STRINGS的pandas列分类为自定义名称的自定义组。类似于cuts函数,但适用于字符串。
例如,使用列表列表定义什么组。
grouping_lists = [['Pakistan', 'China', 'Iran'], ['Germany', 'UK', 'Poland'],
['Australia'], ['USA']]
对应于名称['Asia', 'Europe', 'Australia', 'Other']
。
并且如果列表中不存在某些内容,则将其标记为'Other'
或其他内容。
示例:
my_id country_name
0 100 Pakistan
1 200 Germany
2 140 Australia
3 400 Germany
4 225 China
5 125 Pakistan
6 600 Poland
7 0 Austria
my_id country_name Groups
0 100 Pakistan Asia
1 200 Germany Europe
2 140 Australia Australia
3 400 Germany Europe
4 225 China Asia
5 125 Pakistan Asia
6 600 Poland Europe
7 0 Austria Other
代替合并答案,您可以将分组列表修改为字典,然后使用pandas.Series.map
pandas.Series.map
这是您无需手动创建map dict的方式(如果它很大的话:]]
country_map = {
'Pakistan': 'Asia', 'China': 'Asia',
'Iran': 'Asia', 'Germany': 'Europe',
'UK': 'Europe', 'Poland': 'Europe',
'Australia': 'Australia', 'USA': 'Other'
}
df.assign(Groups=df.country_name.map(country_map)).fillna('Other')
my_id country_name Groups
0 100 Pakistan Asia
1 200 Germany Europe
2 140 Australia Australia
3 400 Germany Europe
4 225 China Asia
5 125 Pakistan Asia
6 600 Poland Europe
7 0 Austria Other