0

I would like to add a column to a pandas dataframe based on the value from another dataframe. Here is table 1 and table 2. I would like to update the duration for table 1 based on the value from table 2. Eg, row 1 in Table 1 is Potato, so the duration should be updated to 30 based on value from table 2.

Table 1

Crops Entry Time Duration
Potato 2022-03-01 0
Cabbage 2022-03-02 0
Tomato 2022-03-03 0
Potato 2022-03-0 0

Table 2

Crops Duration
Potato 30
Cabbage 20
Tomato 25

Thanks.

1 Answers1

2

Just use merge method:

df = df1.merge(df2, on='Crops', how='left')

Before doing that I suggest to drop the duration column in the first dataframe (df1).

The parameter 'on' defines on which column you want to merge (also called 'key') and how='left' it returns a dataframe with the length of the first dataframe. Imposing 'left' avoids that records with 'vegetables'in df1 that are not present in df2 will be deleted.

Google 'difference between inner, left, right and outer join'.

Costas
  • 118
  • 6