从 python 中的日期开始对月份和日期进行分组
Grouping Month and day from the Date in python
我有这样的数据框
Date Temerature
2016-01-01 3
2017-01-01 4
2016-02-01 5
2017-02-01 7
2016-03-01 2
2017-03-01 4
现在,我想像这样获取基于月份和日期的平均温度温度
Date Temperature
Jan 1 3.5
Feb 1 6
Mar 1 3
所以,我想制作一个像这样的新数据框,在 python 中如何实现?
尝试:
df['Date'] = pd.to_datetime(df['Date'])
df.groupby(df['Date'].dt.strftime('%B %d'), sort=False).mean()
输出:
Temerature
Date
January 01 3.5
February 01 6.0
March 01 3.0
试试这个:
import pandas as pd
df = pd.DataFrame({'Date': ['2016-01-01', '2017-01-01', '2016-02-01', '2017-02-01', '2016-03-01', '2017-03-01'],
'Temerature': [3, 4, 5, 7, 2, 4]})
df['Date'] = pd.to_datetime(df['Date'])
df['Month'] = df['Date'].dt.month
df['Day'] = df['Date'].dt.day
df.groupby(['Month', 'Day'])['Temerature'].mean().reset_index()
你的table:
import pandas as pd
df = pd.DataFrame({
"Date":["2016-01-01","2017-01-01","2016-02-01","2017-02-01","2016-03-01 ","2017-03-01 "],
"Temerature":[3,4,5,7,2,4]
})
转换数据:
df["Date"] = pd.to_datetime(df["Date"], infer_datetime_format=True)
df["Date"] = df["Date"].dt.strftime('%B %d')
df = df.groupby(["Date"], sort=False).mean().reset_index()
输出:
print(df)
Date Temerature
0 January 01 3.5
1 February 01 6.0
2 March 01 3.0
我有这样的数据框
Date Temerature
2016-01-01 3
2017-01-01 4
2016-02-01 5
2017-02-01 7
2016-03-01 2
2017-03-01 4
现在,我想像这样获取基于月份和日期的平均温度温度
Date Temperature
Jan 1 3.5
Feb 1 6
Mar 1 3
所以,我想制作一个像这样的新数据框,在 python 中如何实现?
尝试:
df['Date'] = pd.to_datetime(df['Date'])
df.groupby(df['Date'].dt.strftime('%B %d'), sort=False).mean()
输出:
Temerature
Date
January 01 3.5
February 01 6.0
March 01 3.0
试试这个:
import pandas as pd
df = pd.DataFrame({'Date': ['2016-01-01', '2017-01-01', '2016-02-01', '2017-02-01', '2016-03-01', '2017-03-01'],
'Temerature': [3, 4, 5, 7, 2, 4]})
df['Date'] = pd.to_datetime(df['Date'])
df['Month'] = df['Date'].dt.month
df['Day'] = df['Date'].dt.day
df.groupby(['Month', 'Day'])['Temerature'].mean().reset_index()
你的table:
import pandas as pd
df = pd.DataFrame({
"Date":["2016-01-01","2017-01-01","2016-02-01","2017-02-01","2016-03-01 ","2017-03-01 "],
"Temerature":[3,4,5,7,2,4]
})
转换数据:
df["Date"] = pd.to_datetime(df["Date"], infer_datetime_format=True)
df["Date"] = df["Date"].dt.strftime('%B %d')
df = df.groupby(["Date"], sort=False).mean().reset_index()
输出:
print(df)
Date Temerature
0 January 01 3.5
1 February 01 6.0
2 March 01 3.0