Reputation: 3
I would love some guidance how I can compare the same dates over different years. I have daily mean temperature data for all March days from 1997-2018 and my goal is to see the mean temperature of each day over my time period. My df is simple and the head and tail looks like the following:
IndexType = Datetime
Date temp
1997-03-01 6.00
1997-03-02 6.22
1997-03-03 6.03
1997-03-04 4.41
1997-03-05 5.29
Date temp
2018-03-27 -2.44
2018-03-28 -1.01
2018-03-29 -1.08
2018-03-30 -0.53
2018-03-31 -0.11
I imagine the goal could be either 1) a dataframe with days as an index and years as column or 2) a Series with days as index and and the average daily temperature of 1997-2018.
df = pd.read_csv(file, sep=';', skiprows=9, usecols=[0, 1, 2, 3], parse_dates=[['Datum', 'Tid (UTC)']], index_col=0)
print(df.head())
df.columns = ['temp']
df.index.names = ['Date']
df_mar = df.loc[df.index.month == 3]
df_mar = df_mar.resample('D').mean().round(2)
Upvotes: 0
Views: 273
Reputation: 3722
You can use groupby
to see lots of comparisons. Not sure if that's exactly what you're looking for?
Make sure your date column is a Timestamp
.
import pandas as pd
df = df.reset_index(drop=False)
df['Date'] = pd.to_datetime(df['Date'])
I'll initialize a dataframe to practice on:
import datetime
import random
base = datetime.datetime.today()
date_list = [base - datetime.timedelta(days=x) for x in range(0, 100000)]
df = pd.DataFrame({'date':date_list, 'temp':[random.randint(-30, 100) for x in range(100000)]})
march = df[df['date'].dt.month == 3]
g = march.groupby(march['date'].dt.day).agg({'temp':['max', 'min', 'mean']})
alternatively you can do this across your whole dataframe, not just march.
df.groupby(df['date'].dt.month).agg({'temp':['max', 'min', 'mean', 'nunique']})
temp
max min mean nunique
date
1 100 -30 34.999765 131
2 100 -30 35.167485 131
3 100 -30 35.660215 131
4 100 -30 34.436264 131
5 100 -30 35.424371 131
6 100 -30 35.086253 131
7 100 -30 35.188133 131
8 100 -30 34.772781 131
9 100 -30 34.839173 131
10 100 -30 35.248528 131
11 100 -30 34.666302 131
12 100 -30 34.575583 131
Upvotes: 1