Skip to content Skip to sidebar Skip to footer

Generate Multiple Columns Based On Conditions From Other Columns

I've searched quite a bit for a solution, but almost all questions are related to the creation of a single column. So, here is my problem. Given an example DataFrame: df = pd.Data

Solution 1:

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

Post a Comment for "Generate Multiple Columns Based On Conditions From Other Columns"