简体   繁体   中英

Pandas: writing values to a target column based on values in a source column without overwriting any existing values in the target column

I have the following data frame:

import pandas as pd
import numpy as np

df = pd.DataFrame({'Manufacturer':['Mercedes', 'BMW', 'Mercedes', 'Audi', 'Honda', 'Aston Martin', 'Audi', 'Jeep', 'Land Rover'],
                       'Color':['Blue', 'White', 'Black', 'Green', 'Red', 'White', 'Silver', 'Silver', 'Blue'],
                       'Country':['United States', '["United States", "Mexico"]', 'Ireland', 'Japan', '["United States","Canada"]', 'Sweden', 'United Kingdom', 'United Kingdom', '["Brazil","United States","Canada"]'],
                       'Region':['Americas','','Europe','Asia','','Europe', 'Europe', 'Europe', '']    
                  })


    Manufacturer    Color   Country                               Region
0   Mercedes        Blue    United States                         Americas
1   BMW             White   ['United States','Mexico']  
2   Mercedes        Black   Ireland                               Europe
3   Audi            Green   Japan                                 Asia
4   Honda           Red     ['Canada','United States']  
5   Aston Martin    White   Sweden                                Europe
6   Audi            Silver  United Kingdom                        Europe
7   Jeep            Silver  United Kingdom                        Europe
8   Land Rover      Blue    ['Brazil','United States','Canada']  

I would like to write "Americas" to the Region column if:

a) there is no existing value in the Region column, and

b) the Country column has "United States" somewhere in the string

It's possible to use np.where , as follows:

df['Region'] = np.where(df['Country'].str.contains('United States'), 'Americas', '**ERROR**')

But, this approach overwrites the existing values in the Region column:

    Manufacturer    Color   Country                                Region
0   Mercedes        Blue    United States                          Americas
1   BMW             White   ["United States", "Japan"]             Americas
2   Mercedes        Black   Ireland                                **ERROR**
3   Audi            Green   Japan                                  **ERROR**
4   Honda           Red     ["United States","Canada"]             Americas
5   Aston Martin    White   Sweden                                 **ERROR**
6   Audi            Silver  United Kingdom                         **ERROR**
7   Jeep            Silver  United Kingdom                         **ERROR**
8   Land Rover      Blue    ["Brazil","United States","Canada"]    Americas

What's the best way to do this without overwriting any existing values in the Region column?

Thanks in advance!

You can easily do with your own approach by a little twisting in your code. I hope this code will solve your problem:


df['Region'] = np.where((df['Region'].isnull()|(df['Region']==''))&(df['Country'].str.contains('United States')), 'Americas', df['Region'])

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