Subtract A Column In Pandas Dataframe By Its First Value
I need to subtract all elements in one column of pandas dataframe by its first value. In this code, pandas complains about self.inferred_type, which I guess is the circular referen
Solution 1:
I think you can select first item in column Time
by iloc
:
df.Time = df.Time - df.Time.iloc[0]
Sample:
start = pd.to_datetime('2015-02-24 10:00')
rng = pd.date_range(start, periods=5)
df = pd.DataFrame({'Time': rng, 'a': range(5)})
print (df)
Time a
0 2015-02-24 10:00:00 0
1 2015-02-25 10:00:00 1
2 2015-02-26 10:00:00 2
3 2015-02-27 10:00:00 3
4 2015-02-28 10:00:00 4
df.Time = df.Time - df.Time.iloc[0]
print (df)
Time a
0 0 days 0
1 1 days 1
2 2 days 2
3 3 days 3
4 4 days 4
Notice:
For me works perfectly your 2 ways also.
Post a Comment for "Subtract A Column In Pandas Dataframe By Its First Value"