How to substract a single value from column of pandas DataFrame

Question:

i have one data frame suppose:

name age hb
ali  34  14
jex  16  13
aja  24  16
joy  23  12

i have a value say “5” that i want to substract from each member of column “hb”

new column could be:

hb
9
8
11
7

What is the best method to do this…

thanks and regards.

Asked By: jax

||

Answers:

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
Answered By: Anand S Kumar

try this:

df["hb"] - 5

df["hb"] will select hb column and subtract 5 from it

Answered By: Hackaholic

You can also do this using the pandas.apply function

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

Answered By: Colin Anthony

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
Answered By: Durmus

If you want this subtraction to be saved in your DataFrame and avoid the old SettingWithCopyWarning, use loc:

df.loc["hb"] -= 5

Importantly, if you need to use multiple conditions for selecting a value range, put both into the loc call (chaining does not work for this):

df.loc[df.age==34,"hb"] -= 5
Answered By: smcs

eval lets you assign the new values directly to your existing column hb:

In [6]: df.eval("hb = hb - 5", inplace=True)

In [7]: df
Out[7]: 
  name  age  hb
0  ali   34   9
1  jex   16   8
2  aja   24  11
3  joy   23   7

Since inplace=True you don’t need to assign it back to df.

Answered By: rachwa
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.