本文へスキップ
hdknr blog
戻る

pandas SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.

pandas : SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.

どちらでも Warning:

    def _balance(df):
        if df["bookitem_kind"] == "1":
            return -1 * df["amount"]
        return df["amount"]

    df_epm_cost["amount2"] = df_epm_cost.apply(_balance, axis=1)
df_epm_cost["amount"].where(df_epm_cost["bookitem_kind"] == "0", -1 * df_epm_cost["amount"], inplace=True)

ValueError: cannot reindex on an axis with duplicate labels

df_epm_cost.loc[df_epm_cost["bookitem_kind"] == "1", "amount2"] = df_epm_cost["amount"] * (-1)

インデックスを削除するとうまく行く:

df_epm_cost = df_epm_cost.reset_index(drop=True)

インデックスを削除すると where でもうまく行く:

df_epm_cost["amount"].where(df_epm_cost["bookitem_kind"] == "0", -1 * df_epm_cost["amount"], inplace=True)

記事



前の記事
Django 動的モデル生成
次の記事
DRF: DataFrame を使って CSV エクスポート