ScientiaEtVeritas
ScientiaEtVeritas

Reputation: 5278

Pandas: Subtract Groupwise Minimum

I have a dataframe df with a column date and from this I want to subtract the groupwise (grouped by PID) minimum of the date.

So I've calculated the groupwise minimum like this: df.groupby(by="PID").min()

And wanted to subtract it where I'm stucked: df['date'] = df['date'] - ???

What's the next step here? Or are there any methods that are easier?

My data looks somehow like this:

----------------------------
|   PID   |  DATE          |
----------------------------
|    5    |  2017-05-05    |
|    7    |  2016-10-04    |
|    7    |  2017-05-03    |
|    5    |  2016-12-20    |
|    5    |  2017-02-01    |

Upvotes: 2

Views: 145

Answers (1)

jezrael
jezrael

Reputation: 862591

I think you want transform, which return Series with same index as df, so is possible subtract column:

print (df.groupby(by="PID")['DATE'].transform('min'))
0   2016-12-20
1   2016-10-04
2   2016-10-04
3   2016-12-20
4   2016-12-20
Name: DATE, dtype: datetime64[ns]

df['new'] = df['DATE'] - df.groupby(by="PID")['DATE'].transform('min')
print (df)
   PID       DATE      new
0    5 2017-05-05 136 days
1    7 2016-10-04   0 days
2    7 2017-05-03 211 days
3    5 2016-12-20   0 days
4    5 2017-02-01  43 days

Upvotes: 3

Related Questions