Reputation: 43
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?
Upvotes: 0
Views: 179
Reputation: 242
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]
Upvotes: 1