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.
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
try this:
df["hb"] - 5
df["hb"]
will select hb
column and subtract 5 from it
You can also do this using the pandas.apply function
df.loc[:, "hb"] = df["hb"].apply(lambda x: x - 5)
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
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
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
.
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.
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
try this:
df["hb"] - 5
df["hb"]
will select hb
column and subtract 5 from it
You can also do this using the pandas.apply function
df.loc[:, "hb"] = df["hb"].apply(lambda x: x - 5)
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
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
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
.