0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

販売データ分析チートシート (前処理/集計編)

Posted at

EC系の販売データを分析する際に使う処理

販売データは以下のcsvを想定

userid order_id purchase_date shipment_date product_name amount
1 1 2021-01-01 00:00:00 2021-01-03 00:00:00 A 100
2 2 2021-01-01 00:00:00 2021-01-03 00:00:00 A 100
3 3 2021-01-01 00:00:00 2021-01-03 00:00:00 B 200
1 4 2021-02-01 00:00:00 2021-02-03 00:00:00 B 200
2 5 2021-02-01 00:00:00 2021-02-03 00:00:00 A 100
3 6 2021-02-01 00:00:00 2021-02-03 00:00:00 B 200

csv読み込み

import pandas as pd
import numpy as np

df = pd.read_csv('sales.csv', header=0)
# xlsx形式の場合は以下
# df = pd.read_excel('sales.xlsx')

欠損値補完

  • [userid]が欠損していたら'guest'で補完
df['userid'] = df['userid'].fillna('guest')

欠損レコードの削除

  • [sihpment_date]が欠損しているレコードを削除
  • inplace=Trueで元のdfを変更
df.dropna(subset=['shipment_date'], inplace=True)

ソート

  • [userid]昇順, かつ, [purchase_date]昇順
df = df.sort_values(['userid', 'purchase_date'], ascending=[True, True])
               index  userid  order_id purchase_date shipment_date  ...省略...
purchase_date                                                        
2021-01-01         0       1         1    2021-01-01    2021-01-03   
2021-02-01         3       1         4    2021-02-01    2021-02-03   
2021-01-01         1       2         2    2021-01-01    2021-01-03   
2021-02-01         4       2         5    2021-02-01    2021-02-03   
2021-01-01         2       3         3    2021-01-01    2021-01-03   
2021-02-01         5       3         6    2021-02-01    2021-02-03

キャスト

# Seriesをdatetimeにキャスト(以下の例だとdatetime64型(ns)にキャスト)
df['purchase_date'] = pd.to_datetime(df['purchase_date'])
df['shipment_date'] = pd.to_datetime(df['shipment_date'])

# useridをintにキャスト(astypeメソッドで'int'を指定するとint32にキャスト)
# 今回の例では[userid]は既にint64型だが、
df['userid'] = df['userid'].astype('int')

Series内各要素の置換

  • [product_name]の半角スペースを削除
df['product_name'] = df['product_name'].str.replace(' ', '')

会員購入と非会員購入(ゲスト購入)の分別(dfを分ける)

df_guest = df.query('userid == "guest"')
df = df.query('userid != "guest"')

[userid]毎の購入回数[cumcount]カラムを追加

df['cumcount'] = df.groupby('userid').cumcount() + 1

indexつけ直し

df = df.reset_index()
df_guest = df_guest.reset_index()

indexを購入日にする

  • indexを購入日にすると[yyyy-mm-dd:yyyy-mm-dd]形式でスライスできるので便利
df.set_index('purchase_date', drop=False, inplace=True)
print(df[2021-01-01:2021-01-31]
               index  userid  order_id purchase_date shipment_date  ...省略...
purchase_date                                                        
2021-01-01         0       1         1    2021-01-01    2021-01-03   
2021-01-01         1       2         2    2021-01-01    2021-01-03   
2021-01-01         2       3         3    2021-01-01    2021-01-03   
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?