I have a weather dataset, it gave me many years of data, as below:
| Date | rainfallInMon |
|---|---|
| 2009-01-01 | 0.0 |
| 2009-01-02 | 0.03 |
| 2009-01-03 | 0.05 |
| 2009-01-04 | 0.05 |
| 2009-01-05 | 0.06 |
| ... | ... |
| 2009-01-29 | 0.2 |
| 2009-01-30 | 0.21 |
| 2009-01-31 | 0.21 |
| 2009-02-01 | 0.0 |
| 2009-02-02 | 0.0 |
| ... | ... |
I am trying to get the daily rainfall, starting from the end of the month subtracting the previous day. For eg:
| Date | rainfallDaily |
|---|---|
| 2009-01-01 | 0.0 |
| 2009-01-02 | 0.03 |
| 2009-01-03 | 0.02 |
| ... | ... |
| 2009-01-29 | 0.01 |
| 2009-01-30 | 0.0 |
| ... | ... |
Thanks for your efforts in advance.
CodePudding user response:
Try:
# Convert to datetime if it's not already the case
df['Date'] = pd.to_datetime(df['Date'])
df['rainfallDaily'] = df.groupby(df['Date'].dt.month)['rainfallInMon'].diff().fillna(0)
print(df)
# Output
Date rainfallInMon rainfallDaily
0 2009-01-01 0.00 0.00
1 2009-01-02 0.03 0.03
2 2009-01-03 0.05 0.02
3 2009-01-04 0.05 0.00
4 2009-01-05 0.06 0.01
5 2009-01-29 0.20 0.14
6 2009-01-30 0.21 0.01
7 2009-01-31 0.21 0.00
8 2009-02-01 0.00 0.00
9 2009-02-02 0.00 0.00
CodePudding user response:
I have a weather dataset, it gave me many years of data:
df['rainfallDaily'] = df.groupby(df['Date'].dt.to_period('m'))['rainfallInMon'].diff().fillna(0)
Or:
df['rainfallDaily'] = df.groupby(pd.Grouper(freq='M',key='Date'))['rainfallInMon'].diff().fillna(0)
