Last active
September 8, 2020 15:53
-
-
Save tana9/d3bb2229fc11c99cce969f1dd42875a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import codecs | |
import pandas as pd | |
import numpy as np | |
# Shift-JISを読み込み | |
with codecs.open("sample.csv","r", encoding="sjis",errors="ignore") as f: | |
df = pd.read_csv(f) | |
# 列を日付型に変換 | |
df["date"] = pd.to_datetime(df["date"]) | |
# col1のデータがなければ、col2の値を代入 | |
df["col3"] = np.where(df["col1"] is None, df["col2"], df["col1"]) | |
# 日付を年月形式に変換 | |
df["year_month"] = df["date"].dt.strftime("%Y%m") | |
# 集計 | |
pivot = pd.pivot_table(df, value="uriage", index="kokyaku", column="date", aggfunc="sum") | |
# Shift-JISで書き込み | |
with codecs.open("a.csv", "w", encoding="sjis", errors="ignore") as f: | |
pivot.to_csv(f) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment