简体   繁体   中英

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM