Home > database >  How to replace values in a column from another column in a dataframe based on certain conditions usi
How to replace values in a column from another column in a dataframe based on certain conditions usi

Time:10-29

df1:

Name                      Code
2000667 - APPLE IPHONE    2000667
2000667 - APPLE IPHONE    2000667
2005565 - APPLE IPHONE    CASE
2005565 - APPLE IPHONE    CASE
                          2005450
                          2002130
                          2637440
                          2637409
2003598 - SAMSUNG GALAXY  2113521
2003598 - SAMSUNG GALAXY  HEADSET
                          2637437
2003639 - APPLE IPHONE    2003639
2005565 - APPLE IPHONE    SCREEN

Expected Output:

DF2:

Name                        Code
2000667 - APPLE IPHONE      2000667
2000667 - APPLE IPHONE      2000667
CASE - APPLE IPHONE         CASE
CASE - APPLE IPHONE         CASE
                            2005450
                            2002130
                            2637440
                            2637409
2113521 - SAMSUNG GALAXY    2113521
HEADSET - SAMSUNG GALAXY    HEADSET
                            2637437
2003639 - APPLE IPHONE      2003639
SCREEN - APPLE IPHONE       SCREEN

I want to update the Code in the name column based on the value in 'Code' column of the dataframe, if the value in the Name column is blank, leave it as blank. If the code in the Name column and Code column are different I want to replace the code in the Name column with the value in the Code column.

Tried this code:

df1['Name'].apply(lambda x: x.replace(df1['Code']) if df1['Code'] in x else ''])

CodePudding user response:

Perhaps you could use the Code column plus everything after the first space for non-null columns.

Note: this would actually work without the .isnull() filter, but I would recommend leaving it so that it's more clear what you're trying to do.

df.loc[~df['Name'].isnull(), 'Name'] = df['Code']   " "   df['Name'].str.split(' ', n=1).str[-1]
  • Related