How to substract a single value from column of pandas DataFrame

You can also do this using the pandas.apply function

df.loc[:, "hb"] = df["hb"].apply(lambda x: x - 5)


Simply subtract the scalar value from the pandas.Series , for numerical columns pandas would automatically broadcast the scalar value and subtract it from each element in the column. Example -

df['hb'] - 5 #Where `df` is your dataframe.

Demo -

In [43]: df
Out[43]:
  name  age  hb
0  ali   34  14
1  jex   16  13
2  aja   24  16
3  joy   23  12

In [44]: df['hb'] - 5
Out[44]:
0     9
1     8
2    11
3     7
Name: hb, dtype: int64

If you are using this:

df['hb'] - 5

you will get a new single column. But if you want to keep the rest then you have to use:

df['hb'] -= 5

Tags:

Python

Pandas