Home > Enterprise >  Replace a particular string in a column with strings in another column in Pandas
Replace a particular string in a column with strings in another column in Pandas

Time:12-02

I wonder how to replace the string value of 'Singapore' in location1 column with the string values from location2 column. In this case, they're Tokyo, Boston, Toronto and Hong Kong, Boston.

import pandas as pd
data = {'location1':["London, Paris", "Singapore", "London, New York", "Singapore", "Boston"], 
        'location2':["London, Paris", "Tokyo, Boston, Toronto", "London, New York", "Hong Kong, Boston", "Boston"]}

df = pd.DataFrame(data)

          location1               location2
0     London, Paris           London, Paris
1         Singapore  Tokyo, Boston, Toronto
2  London, New York        London, New York
3         Singapore       Hong Kong, Boston
4            Boston                  Boston

CodePudding user response:

Simply, use .loc and indexing:

df.loc[df['location1'].eq('Singapore'), 'location1'] = df['location2']
print(df)

# Output:
                location1               location2
0           London, Paris           London, Paris
1  Tokyo, Boston, Toronto  Tokyo, Boston, Toronto
2        London, New York        London, New York
3       Hong Kong, Boston       Hong Kong, Boston
4                  Boston                  Boston

CodePudding user response:

We can do it using the numpy.where method :

>>> import numpy as np
>>> df["location1"] = np.where(df["location1"] == 'Singapore', df["location2"], df["location1"])
>>> df
    location1               location2
0   London, Paris           London, Paris
1   Tokyo, Boston, Toronto  Tokyo, Boston, Toronto
2   London, New York        London, New York
3   Hong Kong, Boston       Hong Kong, Boston
4   Boston                  Boston

CodePudding user response:

Try:

df['location1'] = df['location1'].mask(df['location1'] == 'Singapore')\
                                 .fillna(df['location2'])

Output:

                location1               location2
0           London, Paris           London, Paris
1  Tokyo, Boston, Toronto  Tokyo, Boston, Toronto
2        London, New York        London, New York
3       Hong Kong, Boston       Hong Kong, Boston
4                  Boston                  Boston
  • Related