[英]Dataframe: pandas' isin working in one case but showing error in the other
I have the following code that analyzes the recent COVID19 data and finds cumulative confirmed cases for selected countries, which works fine.我有以下代码可以分析最近的 COVID19 数据并找到选定国家/地区的累积确诊病例,效果很好。
import pandas as pd
import matplotlib.pyplot as plt
url="https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv"
df=pd.read_csv(url)
print(df.head())
print('Dropping province, latitude and longitude')
df = df.drop(['Province/State', 'Lat', 'Long'], axis = 1)
print(df.head())
print('Selecting the countries of interest')
countries=['Italy','Netherlands']
s1=df.loc[df['Country/Region'].isin(countries)]
print('s1=\n',s1.head())
print('Summing all provinces for the same country')
df_gr = s1.groupby('Country/Region').sum()#.reset_index()
print(df_gr.head())
The above gives me at the end:上面给了我最后:
Summing all provinces for the same country
1/22/20 1/23/20 1/24/20 1/25/20 1/26/20 1/27/20 1/28/20 1/29/20 1/30/20 ... 3/26/20 3/27/20 3/28/20 3/29/20 3/30/20 3/31/20 4/1/20 4/2/20 4/3/20
Country/Region ...
Italy 0 0 0 0 0 0 0 0 0 ... 80589 86498 92472 97689 101739 105792 110574 115242 119827
Netherlands 0 0 0 0 0 0 0 0 0 ... 7468 8647 9819 10930 11817 12667 13696 14788 15821
Now I do groupby
first and then do isin
to select two countries' data and expect to obtain the same result:现在我先做
groupby
再做isin
到select两个国家的数据,期望得到同样的结果:
import pandas as pd
import matplotlib.pyplot as plt
url="https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv"
df=pd.read_csv(url)
print(df.head())
print('Dropping province, latitude and longitude')
df = df.drop(['Province/State', 'Lat', 'Long'], axis = 1)
print(df.head())
print('Summing all provinces for the same country')
df_gr = df.groupby('Country/Region').sum()#.reset_index()
print(df.head())
print('Selecting the countries of interest')
countries=['Italy','Netherlands']
s1=df_gr.loc[df_gr['Country/Region'].isin(countries)]
print('s1=\n',s1.head())
However, I get the following error:但是,我收到以下错误:
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 2646, in get_loc
return self._engine.get_loc(key)
File "pandas/_libs/index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Country/Region'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "dr2.py", line 27, in <module>
s1=df_gr.loc[df_gr['Country/Region'].isin(countries)]
File "/usr/local/lib/python3.7/site-packages/pandas/core/frame.py", line 2800, in __getitem__
indexer = self.columns.get_loc(key)
File "/usr/local/lib/python3.7/site-packages/pandas/core/indexes/base.py", line 2648, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/_libs/index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Country/Region'
Any explanation or remedy?有什么解释或补救措施吗?
Your code looks correct.您的代码看起来正确。 Upon pasting it in, unchanged, it ran correctly, under pandas 1.0.3.
将其粘贴后,未更改,它在 pandas 1.0.3 下正确运行。
I recommend you upgrade to these package versions:我建议您升级到这些 package 版本:
# Name Version Build Channel
numpy 1.17.3 py37hde6bac1_0 conda-forge
pandas 1.0.3 py37h94625e5_0 conda-forge
If you do not yet have conda managing package versions for you, it is available from https://conda.io/en/latest/miniconda.html如果您还没有 conda 为您管理 package 版本,可以从https://conda.io/en/latest/miniconda.ZFC35FDC70D5FC69D2698Z83A822C7A5E3 获得
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.