【发布时间】:2020-04-07 12:05:14
【问题描述】:
我想绘制图表来比较我通过字符串输入的每个国家/地区的情况。我从here导入数据
这是我所期待的一个例子:
从那张照片。我为要输入的国家范围输入 N = 3。我一步一步输入每个国家的名字。现在起。如果我想绘制输入我的国家/地区的图表以比较一张图表中的病例数,我该怎么办?
这是我的代码:
import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
#read file
dfi = pd.read_csv('time_series_covid19_confirmed_global.csv', sep=',')
df = dfi.drop(['Province/State','Lat','Long'], axis=1)
df
#sum all the cases of each province/State to 1 row.
df_gr = df.groupby('Country/Region').sum()
time = df_gr.columns.tolist()
df_gr.columns = pd.to_datetime(time)
df_gr.reset_index(inplace = True)
countriesx = df_gr['Country/Region'].unique() #keep each country into list.
countries_list = [x for x in countriesx] #clean the data.
df = df_gr.T
new_header = df.iloc[0]
df = df[1:]
df.columns = new_header
#input the number of countries to compare.
while True:
N = int(input('The number of countries to compare: '))
if N < 1 or N > 185:
print('The value must between1 and 185. Try again.\n')
continue
else:
print('Correct inout.\n')
break
#input name of countries to compare.
cnt = len(countries_list)
choose = [] #For the name of each countries.
for i in range(N):
print(f'Country {i+1} of {N}')
while True:
my_countries = input('Enter the full country name: ')
my_countries = my_countries.capitalize()
countries_list.append(my_countries)
set_checker = set(countries_list)
if len(set_checker) == cnt:
print(f'Country {i+1} confirmed as \'{my_countries}\'.\n')
choose.append(my_countries)
break
else:
print(f'There is no country named \'{my_countries}\'. Try again\n')
countries_list.remove(my_countries)
continue
这个我试过了。
【问题讨论】:
-
检查一下:Python中的一个图中的多个绘图stackoverflow.com/questions/21254472/…
-
非常感谢。现在我可以在你的帮助下解决这个问题。
标签: python pandas matplotlib