I have the following dataframe:
name age year salary1 salary2 salary3 salary4
Rodrigo 28 2021 1945 2312 4567 3214
Neil 26 2021 3546 6657 -3200 1855
Loti 34 5500 4671 3895 5512 7864
...
I would like to create new column that will have list of values of the column salary1,salary2,salary3 and salary4 , when this is the result output:
name age year salary1 salary2 salary3 salary4 new_colum
Rodrigo 28 2021 1945 2312 4567 3214 [1945,2312,4567,3214]
Neil 26 2021 3546 6657 -3200 1855 [3546,6657,-3200,1855]
Loti 34 2021 4671 3895 5512 7864 [4671,3895,5512,7864]
I have tried to concat the relevant column by change the type of each column to string and then add them:
df['new_column'] = df['salary1'].astype(str) + ',' + \
df['salary2'].astype(str) + ',' + \
df['salary3'].astype(str) + ',' + \
df['salary4'].astype(str)
that indeed concat the columns but does not make them a list and also change the type to string while I still need it numerical.
My questionn is how can I cerate new column with list of the four column values?
Try this:
df['new_column'] = df[['salary1', 'salary2', 'salary3', 'salary 4']].values.tolist()