0

I have the following statement to compute the mean of three quiz scores and create a new column based on the computed mean:

scores.loc[:, 'Average'] = scores[['Quiz1', 'Quiz2', 'Quiz3']].mean(axis=1).round(2)

However this leads to the following warning:

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

After the dataframe is changed with the following set of operations, this warning shows up:

pattern = r'(?:^|\D)(\d{7})(?!\d)'
regex = re.compile(pattern)
scores = scores[scores['StudentId'].apply(lambda x: (regex.search(x) != None))]
scores

Any ideas why this filtering based on regex causes this error?

renakre
  • 8,001
  • 5
  • 46
  • 99
  • Problem is in code before `scores.loc[:, 'Average'] = scores[['Quiz1', 'Quiz2', 'Quiz3']].mean(axis=1).round(2)`, how is created `scores` ? there is some filtration? then need `.copy` like `scores = df[df.col == 1].copy()` – jezrael Apr 19 '22 at 12:17
  • @jezrael all changes were directly applied to `scores` dataframe. `scores` were initially created from an excel file. – renakre Apr 19 '22 at 12:48
  • what is code between `cores.loc[:, 'Average'] = scores[['Quiz1', 'Quiz2', 'Quiz3']].mean(axis=1).round(2)` and `read_excel` ? Is possible add to question? – jezrael Apr 19 '22 at 12:52
  • thank you, need `scores = scores[scores['StudentId'].apply(lambda x: (regex.search(x) != None))].copy()` for avoid warning – jezrael Apr 19 '22 at 12:58
  • @jezrael I have identified the statements that led to this warning. Thanks for pointing me in this direction. But, I still cannot understand why they result in this warning? – renakre Apr 19 '22 at 12:58
  • @jezrael why? For other filtering I have done (e.g., `scores = scores[scores['Quiz1'] > 30]`), I never added `.copy()`. Why now? – renakre Apr 19 '22 at 13:00
  • Check [this](https://stackoverflow.com/questions/45035929/creating-new-pandas-dataframe-from-certain-columns-of-existing-dataframe/45035966#45035966) – jezrael Apr 19 '22 at 13:00
  • If no modify `scores` later no warning. But if modify warning is raised. – jezrael Apr 19 '22 at 13:01
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/244019/discussion-between-renakre-and-jezrael). – renakre Apr 19 '22 at 13:01
  • @jezrael But, scores = scores[scores['Quiz1'] > 30]) this also modifies it, but will not cause the same error? – renakre Apr 19 '22 at 13:04
  • modify means created new column so also raise error `scores['new'] = 1` similar like in question `cores.loc[:, 'Average'] = scores[['Quiz1', 'Quiz2', 'Quiz3']].mean(axis=1).round(2)` – jezrael Apr 19 '22 at 13:05
  • `scores = scores[scores['Quiz1'] > 30]` is filtration, it is source of warning if modify later. – jezrael Apr 19 '22 at 13:06

0 Answers0