codedancer
codedancer

Reputation: 1634

Replace a particular string in a column with strings in another column in Pandas

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

Upvotes: 0

Views: 40

Answers (3)

Corralien
Corralien

Reputation: 120559

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

Upvotes: 2

Scott Boston
Scott Boston

Reputation: 153570

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

Upvotes: 1

tlentali
tlentali

Reputation: 3455

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

Upvotes: 2

Related Questions