Home > OS >  Filtering DataFrame by column value in comparison to dictionary value?
Filtering DataFrame by column value in comparison to dictionary value?

Time:10-22

I have a DataFrame, for simplicity reason let's say it has only two columns: 'Date created' and 'Responsible Market'. I also have a dictionary called launch_dates, whose keys are countries that correspond to the 'Responsible Market' column and it's values are dates on which the country had it's product launch.

I now want to filter the DataFrame, so that only rows exist whose 'Date Created' was after the launch date off their responsible market.

I have tried this:

df = df.loc[df['Case created on - Date'] >= launch_dates.get(df.loc['Responsible Market'])]

But it gives me

KeyError: 'Responsible Market'

Where is my mistake?

CodePudding user response:

Use Series.map:

df = df.loc[df['Case created on - Date'] >= df['Responsible Market'].map(launch_dates)]
  • Related