Adding two columns to a dataframe

Hi,

I am trying to add to columns of a dataframe read from a .csv:

dfMammals = pd.read_csv(‘MamFuncDatCSV.csv’)
dfMammals[‘FANM’] = df[‘Diet-Fruit’] + df[‘Diet-Nect’]

But when I look at the results they dont make sense as they are not the sum of the values

‘Diet-Fruit’= 0,0,0,0,0,20,20,20,20,20 dtype: int64
‘Diet-Nect’=0,0,0,0,0,0,0,0,40,0 dtype: int64
‘FANM’=0,20,0,80,80,60,30,10,10,0

Could anyone tell me what may be going on?

I tried dfMammals[‘FANM’] = df[‘Diet-Fruit’] + df[‘Diet-Nect’] And I was expecting the sum of the values from each column into the ‘FANM’

Short explanation, you need to use .loc

dfMammals.loc[:, ‘FANM’] = df[‘Diet-Fruit’] + df[‘Diet-Nect’]

Long explanation can be read here

1 Like

Not OP, but can you explain how those numbers got there anyway? I’ve read the linked documentation but it doesn’t really explain this exact behaviour imo.

.loc aligns axis of two columns properly.

Thank you! all sorted now