Python 向数据框上的组添加列

Python 向数据框上的组添加列,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个数据帧: id | x | y 1 | 0.3 | 0.4 1 | 0.2 | 0.5 2 | 0.1 | 0.6 2 | 0.9 | 0.1 3 | 0.8 | 0.2 3 | 0.7 | 0.3 如何相对于id列向dataframe添加新列 例如: id | x | y | color 1 | 0.3 | 0.4 | 'green'

我有一个数据帧:

  id  |   x   |   y 
   1  |  0.3  |  0.4
   1  |  0.2  |  0.5
   2  |  0.1  |  0.6
   2  |  0.9  |  0.1
   3  |  0.8  |  0.2
   3  |  0.7  |  0.3
如何相对于id列向dataframe添加新列

例如:

  id  |   x   |   y   |  color
   1  |  0.3  |  0.4  | 'green'
   1  |  0.2  |  0.5  | 'green'
   2  |  0.1  |  0.6  | 'black'
   2  |  0.9  |  0.1  | 'black'
   3  |  0.8  |  0.2  |  'red'
   3  |  0.7  |  0.3  |  'red'

因此,您的函数不返回颜色名称,而是返回RGB值,如果这是您在“颜色”列中想要的,请首先从唯一id值构建字典,并按照注释中提到的@anky_91的方式应用字典

d={x:random_color() for x in df.id.unique()}
df['color']=df['id'].map(d)

可能会很晚,但如果您想要其他选择,这里有一个简单函数的另一种方法:

colors = ['Green', 'Black', 'Red']

def color(data):
    if data['id'] == 1:
        col = colors[0]
    if data['id'] == 2:
        col = colors[1]
    if data['id'] == 3:
        col = colors[2]
    return col

df['Colors'] = df.apply(color, axis = 1)
print(df)

#    id    x    y Colors
# 0   1  0.3  0.4  Green
# 1   1  0.2  0.5  Green
# 2   2  0.1  0.6  Black
# 3   2  0.9  0.1  Black
# 4   3  0.8  0.2    Red
# 5   3  0.7  0.3    Red

如果你有像
d={1:'green',2:'black',3:'red'}
这样的字典,你可以做
df['color']=df['id'].map(d)
我没有字典,我有数百个id,颜色是由一个函数生成的。你怎么知道哪个id得到哪个颜色?生成颜色的函数是什么
df['id'].map(func)
也与函数一起使用。颜色由函数生成:def random_color():return randint(0255)、randint(0255)、randint(0255)