1

This is the original dataframe.

      Country     Date     Confirmed    Death   Recovered   newConfirmed    newDeath    newRecovered
No                              
1   Afghanistan 31/12/2019     0          0         0            0              0           0
2   Afghanistan 01/01/2020     0          0         0            0              0           0
3   Afghanistan 02/01/2020     0          0         0            0              0           0
4   Afghanistan 03/01/2020     0          0         0            0              0           0
5   Afghanistan 20/03/2020     0          0         0            0              0           0

I want to retrieve only Columns 'Country', 'Confirmed', 'Death', and 'Recovered' for the date '2020-03-20'(which is the latest date) from the original DataFrame into a new DataFrame.

How should I do this?

1 Answers1

0

Use DataFrame.loc with filter by boolean indexing witth max values, but first is necessary convert strings to datetimes by to_datetime with dayfirst=True parameter:

df['Date'] = pd.to_datetime(df['Date'], dayfirst=True) 
df1 = df.loc[df['Date'].eq(df['Date'].max()), ['Country', 'Confirmed', 'Death', 'Recovered']]
print (df1)
       Country  Confirmed  Death  Recovered
5  Afghanistan          0      0          0
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252