時間に伴うパターン

Pythonで学ぶ探索的データ分析

Izzy Weber

Curriculum Manager, DataCamp

時間に伴うパターン

divorce = pd.read_csv("divorce.csv")
divorce.head()
  marriage_date  marriage_duration
0    2000-06-26                5.0
1    2000-02-02                2.0
2    1991-10-09                10.0
3    1993-01-02                10.0
4    1998-12-11                7.0               
Pythonで学ぶ探索的データ分析

DateTime データの取り込み

  • DateTime データは Pandas に明示的に指定する必要がある

 

divorce.dtypes
marriage_date         object
marriage_duration    float64
dtype: object
Pythonで学ぶ探索的データ分析

DateTime データの取り込み

divorce = pd.read_csv("divorce.csv", parse_dates=["marriage_date"])
divorce.dtypes
marriage_date        datetime64[ns]
marriage_duration           float64
dtype: object
Pythonで学ぶ探索的データ分析

DateTime 型への変換

  • pd.to_datetime() は引数を DateTime 型に変換

 

divorce["marriage_date"] = pd.to_datetime(divorce["marriage_date"])
divorce.dtypes
marriage_date        datetime64[ns]
marriage_duration           float64
dtype: object
Pythonで学ぶ探索的データ分析

DateTime データの作成

divorce.head(2)
   month  day  year  marriage_duration 
0      6   26  2000                5.0 
1      2    2  2000                2.0
divorce["marriage_date"] = pd.to_datetime(divorce[["month", "day", "year"]])
divorce.head(2)
    month  day  year  marriage_duration  marriage_date 

 0      6   26  2000                5.0     2000-06-26 
 1      2    2  2000                2.0     2000-02-02
Pythonで学ぶ探索的データ分析

DateTime データの作成

  • dt.monthdt.daydt.year で日付の一部を抽出
divorce["marriage_month"] = divorce["marriage_date"].dt.month
divorce.head()
    marriage_date  marriage_duration  marriage_month 
 0     2000-06-26                5.0               6 
 1     2000-02-02                2.0               2 
 2     1991-10-09               10.0              10 
 3     1993-01-02               10.0               1 
 4     1998-12-11                7.0              12
Pythonで学ぶ探索的データ分析

時間パターンの可視化

sns.lineplot(data=divorce, x="marriage_month", y="marriage_duration")
plt.show()

結婚月と結婚期間の関係を示す折れ線グラフ

Pythonで学ぶ探索的データ分析

Passons à la pratique !

Pythonで学ぶ探索的データ分析

Preparing Video For Download...