我试图在将数据聚合为一系列其他数据帧之后处理数据。我在SQL方面有超过十年的经验,但我对PANDAS还是个新手,我发现这样一个简单的请求似乎有一个非常复杂的解决方案,这让我非常沮丧。你知道吗
我想在SQL中做的是:
Select UniqueID, MinDate, DollarValue
From {select UniqueID, Min(date) as MinDate
from DateTable
Join SalesTable
Where DateTable.ServerTime < SalesTable.DateTime
} as MinDateTable
join SalesTable
Where MinDate between '2017-01-07 00:00:00'
and '2017-01-10 00:00:00
我的Jupyter笔记本里有:
# Import the configparser library
import configparser
# Import database stuff
import pymysql
import psycopg2
# Import pandas and numpy - the python data science magical libraries.
import pandas as pd, numpy as np
DateTable = pd.read_sql(SQL, dbConn)
SalesTable = pd.read_sql(dwQuery, dwConn)
merged_df=DateTable.merge(SalesTable,left_on=['UniqueID'],right_on=['UniqueID'],how='inner')
merged_df[merged_df['server_time'] < merged_df['Datetime']]
gb = merged_df.groupby(['UniqueID', 'Datetime'])
这给了我MinDateTable子查询等价物,但是我需要在SalesTable上重新连接它,以获得在服务器事件时间戳之前发生的美元,而gb是一个序列,而不是一个数据帧。你知道吗
下面是我将序列转换为数据帧的尝试:
gb.apply(lambda x: x['server_time'].set_index())
gb_agg = gb.agg({'server_time' : np.max})
gb_agg.apply(lambda x: x.count())
在第一行抛出一个错误:
AttributeError:“Series”对象没有“set\u index”属性
然而,Set\ U索引在其他文章中被标准地描述为将一个系列转换成一个数据帧的方法。你知道吗
欢迎任何建议
应该把它变成一个数据帧。你知道吗
相关问题 更多 >
编程相关推荐