2

I am new to python. I'm want to change all the values in the column 'Starting' from df_2 with the 'Station' column from df_1. I did it by using for loop . But How can I perform this task in simplest way?

df_1:

  ID     Station
0   1      Satose
1   2      Forlango
2   3      poterio
.
.

df_2:

 Rail_Number  Starting      Ending 
     AABDD       3           44433 
     DLRAKA      1           45232
     MiGOMu      2           18756   
      .
      .
Torxed
  • 22,866
  • 14
  • 82
  • 131

1 Answers1

2

I have answered a similar question here :

Replace a value in a dataframe with a value from another dataframe

Step 1: Convert both columns from df_1 into a dictionary by using the following code:

d = dict(zip(df_1.ID,df_1.Station))

Step 2: Now we just need to map this dictionary and df_2:

df_2.Starting = df_1.ID.map(d)
Anshul Vyas
  • 633
  • 9
  • 19