【问题标题】:Count values on dataset attribute with conditions使用条件计算数据集属性上的值
【发布时间】:2021-02-12 01:35:57
【问题描述】:

我需要你的帮助。我的 csv 文件中的数据集如下所示。

Date                       Service
2020/10/27 00:00:00+00     1
2020/10/27 00:00:00+00     1
2020/10/28 00:00:00+00     3
2020/10/28 00:00:00+00     4
2020/10/28 00:00:00+00     5
2020/10/29 00:00:00+00     5
2020/10/29 00:00:00+00     6

我想做这样的输出。

Date                       Service
2020/10/27                 2
2020/10/28                 12
2020/10/29                 11

np:我有一个大约 5 个月的大型数据集

是的,请帮我删除时钟,因为稍后我想用 matplotlib.pyplot 制作时间序列图。感谢您的帮助。

【问题讨论】:

  • df.groupby('Date').sum()

标签: python pandas dataframe csv conditional-statements


【解决方案1】:

您可以使用resamplegroupby 来实现聚合。然后你可以使用 datetime Series 访问器来格式化你想要的日期(年/月/日):

# ensure "Date" is a datetime dtype if it isn't
df["Date"] = pd.to_datetime(df["Date"]) 

# Resample getting the sum of every day in the "Date" column
aggregated_df = df.resample("D", on="Date").sum().reset_index()
aggregated_df["Date"] = aggregated_df["Date"].dt.strftime("%Y/%m/%d")

print(aggregated_df)
         Date  Service
0  2020/10/27        2
1  2020/10/28       12
2  2020/10/29       11

【讨论】:

    猜你喜欢
    • 2020-01-25
    • 2018-02-16
    • 2021-10-08
    • 2016-07-21
    • 2020-02-07
    • 1970-01-01
    • 2017-11-03
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多