合并这些数据框

时间:2019-09-22 19:27:02

标签: python dataframe

我有以下代码。我正在尝试创建一个dataFrame,将交易合并为单日总计,如下所示:

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", parse_dates=['DateTime']) 
# 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'])

per_day    =    a.groupby(a['date'].dt.date)['Burned'].count().reset_index(name='Transactions')
per_day    =    a.groupby(a['date'].dt.date)['Burned'].sum().reset_index(name='burned') 

最后,我想要一个名为per_day的数据框,其中包含日期,交易记录和已刻录数据。当前,对per_day的最后一次调用将覆盖已经存在的内容。当我尝试per_day ['burned']时,我得到:

   '{mgr}'.format(val=len(self.values), mgr=len(self.mgr_locs)))

ValueError: Wrong number of items passed 2, placement implies 1

我认为这是因为它也包含日期。如何将这两行per_day行合并为一行,其中第一列为日期,第二列为交易,第三列为已刻录?这些将仅显示每天的总金额,该总金额取自代码顶部所有交易的转储。

0 个答案:

没有答案