0

I have a filtered dataframe called df_final, I wanted two have additional tabs. For tab 2, I create a new column, then do filterig on that column and save it, then delete the created column and save it as tab 1. Here is the code:

  df_final = df.query(query)
  df_final["Duplicate"] = df_final.duplicated(subset=['DR/ER#'])
  df_tab2 = df_final.loc[df_final['Duplicate']== True]
  df_tab1 = df_final.drop(columns = ['Duplicate', 'DR/ER#'])
  df_tab1.to_excel(writer, sheet_name = 'DR ER')
  df_tab2.to_excel(writer, sheet_name = 'DR ER duplicates')
    

but I get this Error.

<ipython-input-6-4e0fc04c4f3a>:46: 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
  df_final["Duplicate"] = df_final.duplicated(subset=['DR/ER#'])

Please, help me.

3
  • Did you See the caveats in the documentation:... as the message suggested? Try searching SO with the error message, you should get plenty of information. Commented Jul 15, 2021 at 17:05
  • Did you try using the method suggested in the error message? - Try using .loc[row_indexer,col_indexer] = value instead Commented Jul 15, 2021 at 17:09
  • I didn;t get the method, for which I need to do that? I searched the method but didn't get anything on how it works Commented Jul 15, 2021 at 17:20

1 Answer 1

1

df_final is actually a view of the original DataFrame (sharing its data buffer), so you cannot alter it.

If you want to alter df_final, create it as a new DataFrame (with its own data buffer). To do it, run e.g.:

df_final = df.query(query).copy()

and the "link" to the original DataFrame will no longer exist.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.