简体   繁体   中英

Pandas - most efficient way to replace values in one DataFrame from another, when a condition is met

I have two Dataframes of identical Size. For simplicity's sake

    df1 =   
                start      n      end
    0        20200712  50000      20200812
    1        20200714  51000      20200814
    2        20200716  51500      20200816
    3        20200719  53000      20200819
    4        20200721  54000      20200821
    5        20200724  55000      20200824
    6        20200729  57000      20200824
    
    df2 =    
               start       n      end
    0        20200712      0      20200812
    1        20200714     15      20200814
    2        20200716  51500      20200816
    3        20200719  53000      20200819
    4        20200721     30      20200821
    5        20200724  55000      20200824
    6        20200729  57000      20200824

I would like to replace all 'n's in df2 with those in df1 when a condition is met(here n <50)

I have something like this in mind, which works.

df2.loc[df2['n']<50,'n'] = df1['n']

to get

           start      n            end
0        20200712  50000      20200812
1        20200714  51000      20200814
2        20200716  51500      20200816
3        20200719  53000      20200819
4        20200721  54000      20200821
5        20200724  55000      20200824
6        20200729  57000      20200824

What is the most efficient or 'proper' way when i have multiple such 'n'columns?

Put the other n columns in a list. That can be easily done by slicing the list given by:

my_n_columns = list(df2.columns)[1:-1] # slicing adapted to your example

(As the n columns seems to be in between start and end )

Then apply your code to each column through a loop:

for col in my_n_columns:
  df_result = df2.loc[df2[col]<50,col] = df1[col]

The cleaner way:

df2.where(df2 >= 50, df1)

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