0

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?

2
  • Multipe n columns with the same condition, >50, or different conditions for each column? Commented Jan 7, 2022 at 15:06
  • @ScottBoston Same condition Commented Jan 7, 2022 at 15:09

2 Answers 2

1

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]
Sign up to request clarification or add additional context in comments.

1 Comment

I wanted to know if there are perhaps other ways of achieving the same result, without using a loop
1

The cleaner way:

df2.where(df2 >= 50, df1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.