新型コロナウイルスまとめサイトdata.json(集計方法)

サンプル富山県

github.com

欠損日付を0で補間

# IndexはDatetimeIndex
df.asfreq("D", fill_value=0)

pandas.pydata.org

import pandas as pd
import matplotlib.pyplot as plt

import japanize_matplotlib

# 設定

COUNTS_FILE = "toyama_counts.csv"
PATIENTS_FILE = "toyama_patients.csv"

# ダウンロード

!wget "https://docs.google.com/spreadsheets/d/e/2PACX-1vSJuQThafLPC7OPqUC9TbLV1DmSU0x2Co8VZi2Q2ZZCKLJCTayDl6IoXKyK676mzBgpkoKMgpNK1VML/pub?gid=0&single=true&output=csv" -O $PATIENTS_FILE
!wget "https://docs.google.com/spreadsheets/d/e/2PACX-1vSJuQThafLPC7OPqUC9TbLV1DmSU0x2Co8VZi2Q2ZZCKLJCTayDl6IoXKyK676mzBgpkoKMgpNK1VML/pub?gid=574469870&single=true&output=csv" -O $COUNTS_FILE

# 内容確認

## 日別データ


df_counts = pd.read_csv(COUNTS_FILE, index_col="年月日", parse_dates=True, dtype={"備考": "object"})

df_counts.head(10)

## 患者データ

df_kanja = pd.read_csv(PATIENTS_FILE, index_col="No", dtype={"発症日": "object", "年代": "object", "備考": "object"})

df_kanja.head(10)

# 一般

## 検査実施状況


df_counts.tail(1)

## 陽性患者数

#日別
df_counts["陽性人数"].plot.bar()

# 累計
df_counts["陽性人数"].cumsum().plot.bar()

## 検査実施人数

# 日別
df_counts["検査実施人数"].plot.bar()

# 累計
df_counts["検査実施人数"].cumsum().plot.bar()

## 相談件数

# 日別
df_counts["一般相談件数"].plot.bar()

# 累計
df_counts["一般相談件数"].cumsum().plot.bar()

# その他

## 現在患者数

df_counts["現在患者数"] = df_counts["陽性人数"] - df_counts["退院者数"] - df_counts["死亡者数"]

# 日別
df_counts["現在患者数"].plot.bar()

# 累計
df_counts["現在患者数"].cumsum().plot.bar()

## 治療修了者数

# 日別
df_counts["退院者数"].plot.bar()

# 累計
df_counts["退院者数"].cumsum().plot.bar()

## 年代別

age_lists = ["10歳未満", "10代", "20代", "30代", "40代", "50代", "60代", "70代", "80代", "90代以上"]
df_kanja["年代"].value_counts().reindex(age_lists).plot.bar()

## 居住地別

df_kanja["居住地"].value_counts().plot.bar()

## 陽性率

df_positive = df_counts.loc[:, ["検査実施人数", "陽性人数"]].copy()

# 3日間平均
df_positive_3d = df_positive.rolling(window=3).mean()
df_positive_3d["陽性率"] = df_positive_3d["陽性人数"] / df_positive_3d["検査実施人数"] * 100
df_positive_3d["陽性率"].plot(ylim=(0, 50))

# 7日間平均
df_positive_2w = df_positive.rolling(window=7).mean()
df_positive_2w["陽性率"] = df_positive_2w["陽性人数"] / df_positive_2w["検査実施人数"] * 100
df_positive_2w["陽性率"].plot(ylim=(0, 50))

# 補正なし
df_positive["陽性率"] = df_positive["陽性人数"] / df_positive["検査実施人数"] * 100
df_positive["陽性率"].plot(ylim=(0, 50))