gpt4 book ai didi

python - 收集每天的所有交易并报告当天的总花费

转载 作者:太空宇宙 更新时间:2023-11-04 04:02:11 26 4
gpt4 key购买 nike

我有一个看起来像这样的 DataFrame

date    Burned
8/11/2019 7:00 0.0
8/11/2019 7:00 10101.0
8/11/2019 8:16 5.2

我有这个代码:

import pandas as pd 
import numpy as np
# Read data from file 'filename.csv'
# (in the same directory that your python process is based)
# Control delimiters, rows, column names with read_csv (see later)
df = pd.read_csv("../example.csv")
# Preview the first 5 lines of the loaded data



df = df.assign(Burned = df['Quantity'])
df.loc[df['To'] != '0x0000000000000000000000000000000000000000', 'Burned'] = 0.0
# OR:

df['cum_sum'] = df['Burned'].cumsum()
df['percent_burned'] = df['cum_sum']/df['Quantity'].max()*100.0

a=pd.concat([df['DateTime'], df['Burned']], axis=1, keys=['date', 'Burned'])

b=a.groupby(df.index.date).count()

但我收到此错误:AttributeError: 'RangeIndex' object has no attribute 'date'

基本上我想按天对所有这些时间进行排序,因为它全天都有时间戳。我不在乎一天中什么时候发生不同的事情,我只想获得每天“燃烧”的总数。

最佳答案

首先将parse_dates=['DateTime'] 添加到read_csv对于转换列 Datetime:

df = pd.read_csv("../example.csv", parse_dates=['DateTime']) 

或第一列:

df = pd.read_csv("../example.csv", parse_dates=[0]) 

在你的解决方案中是date列,所以需要Series.dt.date:

b = a.groupby(a['date'].dt.date)['Burned'].sum().reset_index(name='Total')

关于python - 收集每天的所有交易并报告当天的总花费,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58046756/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com