我有一个看起来像这样的 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')
