Home > other >  how to add a column based on condition in python pandas
how to add a column based on condition in python pandas

Time:12-01

I have two dataframes where i want to append two columns based on a condition.

I have the two following dataframes: Data 1 Data 2

I want to add TS%_mean and TS%_SD to the other data if the year matches to the first DataFrame so that each player has their mean and standard deviation for that year.

I have tried to itterate through a loop and having some conditions for the addition of columns. Everything I have tried does not work as intended unfourtunately.

CodePudding user response:

Assuming you have df and df2, you can use merge.

df.merge(df2, on = 'Year')

You may also want to study joins. Although the link is SQL-based, you will learn the basics of joining tables in order to create more complex joins.

  • Related