0

I am renaming a column in a pandas df, but get the error "A value is trying to be set on a copy of a slice from a DataFrame. See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy".

I am aware of this error message - a classic. I thought I had understood what causes this error. However, I am confused why this would pop up in this context. I do not see how I would (mistakenly) work on a copy... I am using the column rename function as follows:

match_stats_home_team.rename(columns={"Game Day ID":"Team Day ID"},inplace=True)

The column "Game Day ID" should be, and is, in fact, renamed into "Team Day ID". What am I doing wrong, and how would "good" code look like?

KingOtto
  • 840
  • 5
  • 18
  • 2
    when you define `match_stats_home_team` from another dataframe, put a `.copy()` at the end of that line where you define this dataframe – anky Feb 13 '21 at 16:39
  • I produce it by filtering from `match_stats` as follows: `match_stats_home_team = match_stats.filter(like="_HT")`. Works like a charm with .copy(). Thank you – KingOtto Feb 13 '21 at 16:40
  • Can you share a minimum code example? – Innat Feb 13 '21 at 16:42

0 Answers0