数据框:熊猫在一种情况下起作用,但在另一种情况下显示错误

时间:2020-04-04 22:35:10

标签: python python-3.x pandas dataframe isin

我有以下代码可以分析最近的COVID19数据,并找到选定国家/地区的累计确诊病例,效果很好。

The network path was not found.

以上内容最后给了我

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())

现在我先做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 ,然后做groupby选择两个国家的数据,并希望获得相同的结果:

isin

但是,出现以下错误:

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())

有任何解释或补救办法吗?

1 个答案:

答案 0 :(得分:1)

您的代码看起来正确。 粘贴后,未更改,它在熊猫1.0.3下正确运行。

我建议您升级到以下软件包版本:

# Name                    Version                   Build  Channel
numpy                     1.17.3           py37hde6bac1_0    conda-forge
pandas                    1.0.3            py37h94625e5_0    conda-forge

如果您尚未使用conda管理软件包版本, 它可以从https://conda.io/en/latest/miniconda.html

获得