Reputation: 8418
I have a Series object in Pandas and I would like to subtract a number equal to the first value of the Series from all values of the Series.
For example:
This:
2015-10-01: 5000
2015-10-02: 5005
2015-10-03: 5012
becomes this:
2015-10-01: 0
2015-10-02: 5
2015-10-03: 12
Is this possible with a native Pandas method? Or should I loop through all values and subtract?
Upvotes: 5
Views: 5745
Reputation: 394279
You can access the first row value using iloc[0]
and then just subtract from the rest of the series:
In [5]:
import io
import pandas as pd
t="""2015-10-01 5000
2015-10-02 5005
2015-10-03 5012"""
s = pd.read_csv(io.StringIO(t), index_col=[0], parse_dates=[0], header=None, delim_whitespace=True, squeeze=True)
s
Out[5]:
0
2015-10-01 5000
2015-10-02 5005
2015-10-03 5012
Name: 1, dtype: int64
In [6]:
s - s.iloc[0]
Out[6]:
0
2015-10-01 0
2015-10-02 5
2015-10-03 12
Name: 1, dtype: int64
It's possible to use head(1)
but you need to index the array in order to get the scalar value otherwise you get NaN
for the 2nd row onwards due to index alignment:
In [9]:
s - s.head(1)[0]
Out[9]:
0
2015-10-01 0
2015-10-02 5
2015-10-03 12
Name: 1, dtype: int64
compare with:
In [10]:
s - s.head(1)
Out[10]:
0
2015-10-01 0
2015-10-02 NaN
2015-10-03 NaN
Name: 1, dtype: float64
Upvotes: 8