这是原始数据框。
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
我只想将日期“ 2020-03-20”(这是最新日期)中的“国家”,“已确认”,“死亡”和“已恢复”列从原始数据框中检索到新的数据框中。
我应该怎么做?
使用DataFrame.loc
带有过滤器由boolean indexing
witth最大值的,但首先需要将字符串转换为日期时间to_datetime
与dayfirst=True
参数:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句