I have a df called df_world
with the following shape:
Cases Death Delta_Cases Delta_Death
Country/Region Date
Brazil 2020-01-22 0.0 0 NaN NaN
2020-01-23 0.0 0 0.0 0.0
2020-01-24 0.0 0 0.0 0.0
2020-01-25 0.0 0 0.0 0.0
2020-01-26 0.0 0 0.0 0.0
... ... ... ...
World 2020-05-12 4261747.0 291942 84245.0 5612.0
2020-05-13 4347018.0 297197 85271.0 5255.0
2020-05-14 4442163.0 302418 95145.0 5221.0
2020-05-15 4542347.0 307666 100184.0 5248.0
2020-05-16 4634068.0 311781 91721.0 4115.0
我想按上次记录中“案例”列的值对国家/地区索引进行排序,即比较所有国家/地区在2020-05-16的案例值并返回已排序的国家/地区列表
I thought about creating another df with only the 2020-05-16 values and then use the df.sort_values()
method but I am sure there has to be a more efficient way.
在讨论此问题时,我还尝试仅选择在2020-05-16上有很多案例超过特定值的国家/地区,而我发现这样做的唯一方法是遍历Country索引:
for a_country in df_world.index.levels[0]:
if df_world.loc[(a_country, last_date), 'Cases'] < cut_off_val:
df_world = df_world.drop(index=a_country)
但这是一个很差的方法。
如果有人对如何提高此代码的效率有任何想法,我将非常高兴。
谢谢 :)