Jgallagher
Jgallagher

Reputation: 111

Date sorting in Pandas

I have merged 2 dataframes into one called 'DRG', this dataframe consists of how many times the term 'new detection' or 'pp reviewed' appeared within a certain day and it has a count beside each date. When I merged the tables the dates for 'new detection' and the dates for 'pp reviewed' won't match up because 'pp reviewed' occurs on less dates. How can I make just one date column with the count for 'new detections' and 'pp reviewed' matching up?

My goal is to plot a line graph with the date as the X axis and 'detection' and 'pp reviewed' as 2 different lines.

Please see below the DRG table,

enter image description here

Upvotes: 0

Views: 62

Answers (2)

MAK7
MAK7

Reputation: 125

Assuming the two dataframes before merger are df_A (the one with 'Dates' column) and df_B (the one with 'Date' column), try this:

DRG = pd.merge(df_A, df_B, how='outer', left_on='Dates', right_on='Date')

An Outer merge type is the use union of keys from both frames, similar to a SQL full outer join. This would be necessary in your case because it is possible for date(s) to exist in one dataframe that is/are not existent in the other.

Upvotes: 0

Julien Roullé
Julien Roullé

Reputation: 662

I think the problem comes from how you merged your dataframes. Try to do this like this:

DRG = pd.merge(df1, df2, left_on='Dates', right_on='Date', how='outer')

You should maybe rename your columns as well, since one has Date and the other one Dates. If the merge doesn't work as expected, try to convert you date to datetime using pd.to_datetime and retry the merge.

Upvotes: 2

Related Questions