0

From a particular dataframe I would like to create a dataframe containing the sum of the columns.

df['sum'] =  df.sum(axis=1)

when excuting that line I get the 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
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy

checking that link the talk goes on about ways to use loc in multiindex dataframes. My dataframe is not multinidex.

What is the correct way to assign a column named "sum" with the addition of all the int values of the df?

This would not help to avoid the warning

x = df.sum(axis=1).copy()
df['sum'] =  x 
JFerro
  • 3,203
  • 7
  • 35
  • 88
  • 1
    The question already has answers, sure, but if you are looking for a quick summary (which that thread does NOT give), here's the quick overview: A slice of a dataframe is when you select part of it. Sometimes when you slice, you're then interacting with a copy of the dataframe, not the original any longer. So trying to set a value and do a slice at the same time can make setting the value not happen, hence the warning. To get around it, do something like `x = result.sum(axis=1)` and then `result['sum'] = x` – Vincent Rupp Nov 08 '22 at 14:29
  • Pardon me, that does not fix your problem. Your df must be defined by slicing a previous DataFrame. Instead, add the `.copy()` to where you defined df originally. For example: `df = df_previous.loc[df_previous["columnA"]==0].copy()` Then you won't get the warning later. – Vincent Rupp Nov 08 '22 at 15:30

0 Answers0