且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

根据其他列的条件生成多个列

更新时间:2021-12-03 02:03:14

使用 result_type ='expand' 参数c $ c> DataFrame.apply ,还为分配的列添加嵌套列表:

Use result_type='expand' parameter in DataFrame.apply and also add nested lists for assigned columns:

df[["new_blue", "new_red", "new_yellow", "new_orange"]] = df.apply(gen_col, axis=1, result_type='expand')
print (df)
   blue  red  yellow  orange config  new_blue  new_red  new_yellow  new_orange
0     5    1       3       9  north         5        1           3           9
1     5    7       9       7  south         7        9           7           5
2     4    5       0       3  north         4        5           0           3