2020年9月25日金曜日

Python実践データ分析100本ノックを読んでみてメモ

Python実践データ分析100本ノックを読んでいて、そのまま写経してるとwarningが出てくので、その対応メモ。

warningが出るコードは、下記のコード
customer_clustering['cluster'] = clusters.labels_
で、waringの内容は、
SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
正直、あんまりDataFrameわかってないけど、どうやら値のセットの仕方が悪いよう。

concatするようにしたらwarningは出なくなった。
customer_clustering = pd.concat([customer_clustering, pd.DataFrame({'cluster': clusters.labels_})], axis=1)
↓のパターンでも同様のwaringが出た。これは、一ヶ月前の日付を入れていくのをforで回している処理。
for i in range(len(exit_customer)):
    exit_customer['exit_date'].iloc[i] = exit_customer['end_date'].iloc[i] - relativedelta(months=1)
これはmapを使う。
exit_customer['exit_date'] = exit_customer['end_date'].map(lambda x: x - relativedelta(months=1))
warningがでて困ったりはしてるけれど、内容としては面白く手を動かしながら理解を進めている。