This might be a very simple problem but I can not find the solution: I want to add a new column "col_new" with operations depending on group variables like groupIDs or dates. So depending on the groupID the calculation should change.
Example:
Year col1 col2
0 2019 10 1
1 2019 4 2
2 2019 25 1
3 2018 3 1
4 2017 56 2
5 2017 3 2
- for Year = 2017: col_new = col1-col2
- for Year = 2018: col_new = col1+col2
- for Year = 2019: col_new = col1*col2
Also I want to wrap this up in a for loop.
year = [2017, 2018, 2019]
for x in year:
df["new_col]" = ................
- tried using if-functions <== allways requires an else so it changes all values of the previous iteration
- using .loc and it works but becomes very hard to handle with long and complex conditions
- tried setting index for column Year. This is easy doing but then I am stuck.
import pandas as pd
import numpy as np
d = {'Year': [2019, 2019, 2019, 2018, 2017, 2017],
'col1': [10, 4, 25, 3, 56, 3],
'col2': [1, 2, 1, 1, 2, 2]}
df = pd.DataFrame(data=d) #the example dataframe
df = df.set_index("Year")
print(df)
col1 col2
Year
2019 10 1
2019 4 2
2019 25 1
2018 3 1
2017 56 2
2017 3 2
Now I need something like:
- if 2017 then col1+col2
- if 2018 then col1-col2
- if 2019 then col1*col2