Pandas 与Flask的数据争用:如何使用SQL语言实现这一点?使用熊猫有意义吗?

Pandas 与Flask的数据争用:如何使用SQL语言实现这一点?使用熊猫有意义吗?,pandas,sqlalchemy,data-munging,Pandas,Sqlalchemy,Data Munging,对SQL非常陌生,在这里使用flask和sqlalchemy是我的问题(我希望不会太长) 概述: 我有一个如下结构的SQL表: name vector axis value unit ref ---------------------------------------------------------------- name1 v1 W 46504 psi ref1 nam

对SQL非常陌生,在这里使用flask和sqlalchemy是我的问题(我希望不会太长)

概述: 我有一个如下结构的SQL表:

name    vector       axis       value       unit    ref 
----------------------------------------------------------------    
name1       v1          W       46504       psi     ref1
name1       v1          L       51757       psi     ref1
name1       v2          W         127       psi     another ref
name1       v2          L         403       psi     ref1
name2 ...
我的目标是“取消堆叠”结果,例如只要
unit
ref
相同,我就可以为每个
name
设置一行

e、 g.我希望得到类似于:

name      v1-L      v2-W    v1-L    v2-W    unit    ref
--------------------------------------------------------------
name1    46504               127    403     psi    ref1
name1                127                    psi    another ref
name2...
尝试使用sqlalchemy: 到目前为止,我试图基于“name”加入同一个表——目前,没有检查
unit
ref
--:

使用熊猫的示例: 下面是我使用
pandas
库得到的结果

import pandas as pd
q = session.query(Test).order_by(Test.id) # that is the default table
row2dict = lambda r: {c.name: getattr(r, c.name) for c in r.__table__.columns}
df = pd.DataFrame([row2dict(i) for i in q])
df = df.drop(['id'], axis=1)

df = df.set_index(['ref', 'unit', 'name', 'vector', 'axis']).sort()
df = df.unstack(level=-2).unstack(level=-1)['value'].reset_index()
print(df)

vector          ref unit   name     v1          v2     
axis                                 L      W    L    W
0       another ref  psi  name1    NaN    NaN  NaN  127
1              ref1  psi  name1  51757  46504  403  NaN
2              ref1  psi  name2  23000  27000  523  217
…这与我的期望相差不远


那么,在SQL语言中这样做有意义吗?因此,我的以下问题是:使用
Flask
framework,使用pandas进行数据挖掘有意义吗?或者我应该坚持使用SQL语言吗?

熊猫可能更适合做这种事情。可能有一些更奇特的SQL函数可以执行这样的转换,但我不确定。下面是您执行最简单方法的示例,这只是将每个视图连接在一起。想到这一点的方式是,由于您正在生成具有从数据派生的“虚拟”列的行,这表明正在从更基本的行创建复合行。下面的方法将四个条件中的每一个对应一组行,v1/W,v1/L,v2/W,v2/L。现在,如果在实践中,有任意数量的“vN”,那么熊猫的转化能力可能更合适

from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session, aliased

Base = declarative_base()

class Test(Base):
    __tablename__ = 'test'
    id = Column(Integer, primary_key=True)
    name = Column(String(32))
    vector = Column(String(32))
    axis = Column(String(1))
    value = Column(Float)
    unit = Column(String(16), default='psi')
    ref = Column(String(32))

engine = create_engine('sqlite://', echo=True)
session = Session(engine)

Base.metadata.create_all(engine)

# some data to play with
data = [{'name':'name1', 'vector':'v1', 'axis':'W', 'value':'46504', 'unit':'psi', 'ref':'ref1'},
        {'name':'name1', 'vector':'v1', 'axis':'L', 'value':'51757', 'unit':'psi', 'ref':'ref1'},
        {'name':'name1', 'vector':'v2', 'axis':'W', 'value':'127', 'unit':'psi', 'ref':'another ref'},
        {'name':'name1', 'vector':'v2', 'axis':'L', 'value':'403', 'unit':'psi', 'ref':'ref1'},
        {'name':'name2', 'vector':'v1', 'axis':'L', 'value':'23000', 'unit':'psi', 'ref':'ref1'},
        {'name':'name2', 'vector':'v1', 'axis':'W', 'value':'27000', 'unit':'psi', 'ref':'ref1'},
        {'name':'name2', 'vector':'v2', 'axis':'L', 'value':'523', 'unit':'psi', 'ref':'ref1'},
        {'name':'name2', 'vector':'v2', 'axis':'W', 'value':'217', 'unit':'psi', 'ref':'ref1'},]
for dic in data:
    t = Test(**dic)
    session.add(t)
session.commit()

axis_w = session.query(Test).filter(Test.axis == "W")
axis_l = session.query(Test).filter(Test.axis == "L")
axis_v1_w = axis_w.filter(Test.vector == "v1").subquery()
axis_v1_l = axis_l.filter(Test.vector == "v1").subquery()
axis_v2_w = axis_w.filter(Test.vector == "v2").subquery()
axis_v2_l = axis_l.filter(Test.vector == "v2").subquery()

def join_axes(left, right):
    return and_(
                left.c.unit == right.c.unit,
                left.c.ref == right.c.ref
            )

name_unit_ref = session.query(Test.name, Test.unit, Test.ref).distinct().subquery()

q = session.query(name_unit_ref.c.name,
                axis_v1_w.c.value.label('v1_w'),
                axis_v1_l.c.value.label('v1_l'),
                axis_v2_w.c.value.label('v2_w'),
                axis_v2_l.c.value.label('v2_l'),
                name_unit_ref.c.unit,
                name_unit_ref.c.ref
            ).\
            outerjoin(axis_v1_w, join_axes(name_unit_ref, axis_v1_w)).\
            outerjoin(axis_v1_l, join_axes(name_unit_ref, axis_v1_l)).\
            outerjoin(axis_v2_w, join_axes(name_unit_ref, axis_v2_w)).\
            outerjoin(axis_v2_l, join_axes(name_unit_ref, axis_v2_l))

for row in q:
    print row
对于您的问题,这是一个更“奇特”的SQL。我正在使用SQLServer2008+获取PIVOT命令。不过,我不确定它是否完全涵盖了你专栏的情况

--Setup the table and data
create table #t
(
 name nvarchar(100) not null
,vector char(2) not null
,axis char(1) not null
,value int not null
,unit char(3) not null
,ref nvarchar(100) not null
);

insert into #t values ('name1','v1','W', 46504,'psi','ref1');
insert into #t values ('name1','v1','L', 51757,'psi','ref1');
insert into #t values ('name1','v2','W',   127,'psi','another ref');
insert into #t values ('name1','v2','L',   403,'psi','ref1');

-- Retrieve the data using SQL Server Pivot
-- http://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx
select * 
from 
(select 
  name
 ,vector + '-' + axis as vector_axis
 ,value
 ,unit
 ,ref
 from #t) as t
pivot (sum(value) for vector_axis IN
([v1-w]
,[v1-L]
,[v2-W]
,[v2-L]
)
) as p

好的,太好了!,我仍然需要提高我的SQL技能:-)我最终使用了我比较熟悉的pandas。无论如何,谢谢你的SQL方式。
--Setup the table and data
create table #t
(
 name nvarchar(100) not null
,vector char(2) not null
,axis char(1) not null
,value int not null
,unit char(3) not null
,ref nvarchar(100) not null
);

insert into #t values ('name1','v1','W', 46504,'psi','ref1');
insert into #t values ('name1','v1','L', 51757,'psi','ref1');
insert into #t values ('name1','v2','W',   127,'psi','another ref');
insert into #t values ('name1','v2','L',   403,'psi','ref1');

-- Retrieve the data using SQL Server Pivot
-- http://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx
select * 
from 
(select 
  name
 ,vector + '-' + axis as vector_axis
 ,value
 ,unit
 ,ref
 from #t) as t
pivot (sum(value) for vector_axis IN
([v1-w]
,[v1-L]
,[v2-W]
,[v2-L]
)
) as p