1

I am trying to +1 to a cell in pandas dataframe

staff['pax'][0]= staff['pax'][0]+1

staff is the dataframe name, pax is the column name while 0 is the row I want to +1. However below is 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
  staff['pax'][j]= staff['pax'][j]+1
C:\Mentor_Engine\test.py:53: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame

I think the code is trying to copy the cell instead of +1 to the value in the cell, anyone could advise on how should I do it?

Teo
  • 11
  • 2

2 Answers2

1

Please refer this documentation https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.at.html

df.at[0, 'pax'] = df['pax'][0]+1
Deepan
  • 430
  • 4
  • 13
0

You could use the .loc function :

staff.loc[0,"pax"] += 1
L4ur3nt
  • 98
  • 6