Home > Software design >  How to add a column to dataframe based on condition
How to add a column to dataframe based on condition

Time:10-12

I have a dataframe which has certain columns. I want to add one more column which would be dependent on source column. The dataframe looks like below :

The year_23 column needs to be added based on source value.

Fy_23 is a static value based on the source. I am new to pandas and need some advice on how it can be acheived

CodePudding user response:

Another simple solution would be to create an empty column and modify the values according to the Source column values:

df['year_23'] = np.nan
df['year_23'][df['Source']=='Google'] = 'text1'
df['year_23'][df['Source']=='Bing'] = 'text2'

CodePudding user response:

Using np.where

df["Year_23"] = np.where(df["Source"].eq("Google"), "x", "y")

CodePudding user response:

You can define a function like this:

def do_this(source):
    if Source == 'Google':
       return "The string you want"
    else:
       return "The string you want if above condition is not satisfied"

After that select the column from dataframe on which you want to apply changes and assign it to new column that you want.

df['New Column'] = df['Column'].apply(do_this)
  • Related