Skip to content

Instantly share code, notes, and snippets.

@tana9
Last active September 8, 2020 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tana9/d3bb2229fc11c99cce969f1dd42875a2 to your computer and use it in GitHub Desktop.
Save tana9/d3bb2229fc11c99cce969f1dd42875a2 to your computer and use it in GitHub Desktop.
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