Python 在Pandas中,我有一个数据框,其中有几列定义了一个配置。我想标识具有相同配置的行
出[12]:Python 在Pandas中,我有一个数据框,其中有几列定义了一个配置。我想标识具有相同配置的行,python,pandas,pandas-groupby,cluster-computing,Python,Pandas,Pandas Groupby,Cluster Computing,出[12]: df = pd.DataFrame({'id': [ 101, 102, 103, 104, 105, 106, 107 ], 'color': [ 'blue', 'blue', 'blue', 'red', 'blue', 'red', 'blue' ], 'location': ['there', 'here', 'there', 'here', 'here', 'there', 'here']})
df = pd.DataFrame({'id': [ 101, 102, 103, 104, 105, 106, 107 ],
'color': [ 'blue', 'blue', 'blue', 'red', 'blue', 'red', 'blue' ],
'location': ['there', 'here', 'there', 'here', 'here', 'there', 'here']})
df
我想制作一个按颜色和位置分组的列,如下所示:
id color location
0 101 blue there
1 102 blue here
2 103 blue there
3 104 red here
4 105 blue here
5 106 red there
6 107 blue here
看起来像是groupby().ngroup()
:
输出:
df['group'] = df.groupby(['color','location'], sort=False).ngroup()
看起来像是groupby().ngroup()
:
输出:
df['group'] = df.groupby(['color','location'], sort=False).ngroup()
我将进行分解
id color location group
0 101 blue there 0
1 102 blue here 1
2 103 blue there 0
3 104 red here 2
4 105 blue here 1
5 106 red there 3
6 107 blue here 1
我将进行分解
id color location group
0 101 blue there 0
1 102 blue here 1
2 103 blue there 0
3 104 red here 2
4 105 blue here 1
5 106 red there 3
6 107 blue here 1
分组是否标有字母或数字有关系?(如果超过26组,可能会更容易/更安全一些……)如果这些组用字母标记,或者数字可以吗?(如果超过26组,可能更容易/更安全一些…)