前提
Pythonを用いてPyPIを作成しています。
実現したいこと
実際にPyPIでパッケージ化し、pipでインストール、
その後にターミナル上で
$ [パッケージ名] [国名]
といった感じで入力をするとその国の結果をグラフ表示するプログラムを完成させたいです。
発生している問題・エラーメッセージ
Python
1Traceback (most recent call last): 2 File "/Users/inoseizuru/Desktop/covidill再開発/covidillness.py", line 41, in <module> 3 if d.loc[(d.date==j) & (d.location==i),'total_deaths']: 4 File "/Users/inoseizuru/opt/anaconda3/lib/python3.9/site-packages/pandas/core/generic.py", line 1527, in __nonzero__ 5 raise ValueError( 6ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
該当のソースコード
Python
1import pandas as pd 2import subprocess as sp 3import sys 4import numpy as np 5import matplotlib.pyplot as plt 6sp.call('wget -nc https://covid.ourworldindata.org/data/owid-covid-data.csv', shell=True) 7 8d=pd.read_csv('owid-covid-data.csv') 9d.fillna(0,inplace=True) 10lastday=str(d.date.iloc[-1:]).split()[1] 11print(lastday) 12 13n=len(sys.argv)-1 14print('countries',n) 15 16countries=[] 17for i in range(n): 18 countries.append(sys.argv[i+1]) 19# print(countries) 20 21from datetime import date 22d0 = date(2020, 2, 29) 23d1 = date(int(lastday.split('-')[0]),int(lastday.split('-')[1]),int(lastday.split('-')[2])) 24delta = d1 - d0 25days=delta.days 26 27daysdate=sorted(d.date.unique()) 28daysdate=daysdate[len(daysdate)-days:-1] 29# print(daysdate) 30 31dd=pd.DataFrame( 32 { 33 "date": daysdate, 34 "deaths": range(len(daysdate)), 35 "icu_patients": range(len(daysdate)), 36 }) 37 38for i in countries: 39 print(i) 40 for j in daysdate: 41 if d.loc[(d.date==j) & (d.location==i),'total_deaths']: 42 dd.loc[dd.date==j,'deaths']=d.loc[(d.date==j) & (d.location==i),'total_deaths'] 43 dd.loc[dd.date==j,'icu_patients']=d.loc[(d.date==j) & (d.location==i),'icu_patients'] 44 dd.to_csv(i+'.csv',index=False) 45 46def main(): 47 for i in range(len(countries)): 48 i=pd.read_csv(countries[i]+'.csv') 49 plt.ylabel('ICU') 50 plt.plot(i.date,i.icu_patients) 51 plt.xticks(np.arange(0,days,30*days/770),rotation=90) 52 fig=plt.figure(1) 53 fig.set_size_inches(10,3) 54 plt.legend(countries) 55 plt.savefig('result.png',dpi=fig.dpi,bbox_inches='tight') 56 plt.show() 57 58if __name__ == "__main__": 59 main()
試したこと
調べたところ、.allなどを使うと解決すると書いてあったので実際に以下のように書いて実行してみたところ、
グラフは表示されましたが、作成されたcsvのデータの中身は空で、グラフは何もプロットされていない真っ白の状態でした。
Python
1if all(d.loc[(d.date==j) & (d.location==i),'total_deaths']):
補足情報
色々とググりまくって試行錯誤しながら2日ほど悩んだのですが、
進捗が生まれずといった形です....
もし対処法がわかる方がいらっしゃいましたらご教授願います。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/11/07 19:44
2022/11/07 19:52
2022/11/08 00:34
2022/11/08 14:59
2022/11/08 15:24
2022/11/09 09:05