从Python中的日期对月份和日期进行分组

发布于 2025-01-12 21:51:04 字数 341 浏览 0 评论 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中怎么可能?

I am having a dataframe like this

  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

Now, I want to get the Average temperature temperature based on Month and day like this

 Date      Temperature
Jan 1         3.5
Feb 1         6
Mar 1         3

So, I want to make a new data frame like this, How is it possible in python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拥醉 2025-01-19 21:51:04

尝试:

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

Try:

df['Date'] = pd.to_datetime(df['Date'])
df.groupby(df['Date'].dt.strftime('%B %d'), sort=False).mean()

Output:

             Temerature
Date                   
January 01          3.5
February 01         6.0
March 01            3.0
中性美 2025-01-19 21:51:04

试试这个:

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()

Try this:

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()
故乡的云 2025-01-19 21:51:04

您的表:

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

Your 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]
})

Transform data:

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()

Output:

print(df)  
          Date  Temerature
0   January 01         3.5
1  February 01         6.0
2     March 01         3.0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文