LoginSignup
1
1

More than 3 years have passed since last update.

【エクセル資料からの】データクレンジング 後編(結合編)【備忘】

Posted at

はじめに

前回、こんな感じのエクセルデータから
スクリーンショット 2019-04-18 12.27.13.png

こんな感じの、DataFrameまで落としました。
スクリーンショット 2019-05-09 10.01.36.png

今回は、月ごとに生成された複数のDataFrameを一つのDataFrameまで結合します。

使用データ

前回参照

1行に整形する

まずは、df.stack()をpandas.DataFrameにかけます。
df.stack()にかけるとマルチインデックスのpandas.Seriesになります。
ここにreset_indexをかけることで、DataFrameを1行にのばすことが出来ます。

参考にさせていただいたサイト様

df_201710 = df.stack().reset_index(name="201710")

出力:
スクリーンショット 2019-05-09 10.51.36.png
次に1行になるよう加工します。

# インデックス名にlevel_1,level_2を結合して使用
ind = se["level_0"] + "_" + se["level_1"]

df_201710.drop(["level_0","level_1"],axis=1,inplace=True)
df_201710.index = ind

出力:
スクリーンショット 2019-05-09 10.58.36.png

結合

最後に結合してデータの整形は完了です。

trip = pd.concat([df_201810,df_201807,df_201804,df_201801,df_201710,df_201707,df_201704,df_201701],axis=1).T

出力:
スクリーンショット 2019-05-09 11.03.19.png

gitにソース上がってます。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1