samedi 24 avril 2021

Adding column in dataframe through loop and populate according to content in another column

I have the following dataframe (New_Data) and I want to add a new column depending on the content of column 'merchant'. For example if 'merchant' contains 'AMZ' or 'AMZN', I want the column to return 'Amazon', if 'merchant' contains 'PRIME', I want the column to return 'Video' and so on and so forth until the last row. I would like to do this through a loop.

I have attempted the following which creates a column but I dont know how to combine loop with content if-function:

merchantlength=len(New_Data[['Merchant']])
merchantlength
i=0
for i in range(merchantlength):
    df['newcolumn']="1"

New_Data = pd.concat([ df], axis=1) 
New_Data

Dataframe

Aucun commentaire:

Enregistrer un commentaire