删除SQLAlchemy或Pandas中的空格

时间:2015-10-22 20:35:00

标签: python pandas sqlalchemy

我使用SQLAlchemy从MSSQL数据库中查询数据,然后使用pandas保存为excel文件。我正在寻找类似于T-SQL的RTRIM的东西,以便从我的数据中删除任何尾随空格。

我知道如何从列标题中删除空格,而不是从数据本身中删除空格。所以我要么在查询时要么删除空格,要么在它是一个pandas数据框时删除空格,但是我没有任何想法(因为大多数搜索都会在解析时检索如何删除空格,而不是写入数据)。

到目前为止我的代码是:

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session,sessionmaker
from sqlalchemy import (Column, Integer, String, Boolean, ForeignKey, DateTime, Sequence, Float)
from sqlalchemy import create_engine
import pandas as pd
import openpyxl


pd.core.format.header_style = None 
pd.core.format.number_format = None 

def data_frame(query, columns):
    def make_row(x):
        return dict([(c, getattr(x, c)) for c in columns]) 
    return pd.DataFrame([make_row(x) for x in query])

engine = create_engine('mssql+pyodbc://u:pass@MyServer/MYDBt?driver=SQL Server', echo=False)
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()

class Tranv(Base):
    __tablename__ = "Transactions"

    part_number = Column(String(20), primary_key=True)
    time_stamp = Column(String(20))
    employee_number = Column(String(6))
    action = Column(String(20)) 

newvarv = session.query(Tranv).filter_by(employee_number='001841').filter_by(time_stamp='2015-10-01 10:49:53.230')

dfx = data_frame(newvarv, [c.name for c in Tranv.__table__.columns])
dfx.columns = dfx.columns.str.strip()
dfx = dfx.rename(columns=lambda x: x.strip())

writer = pd.ExcelWriter('C:\\Users\\grice\\Desktop\\Auto_Scrap_Report\\testy.xlsx')
writer.date_format = None
writer.datetime_format = None

dfx.to_excel(writer, sheet_name='Sheet1', index=False)
writer.save()

1 个答案:

答案 0 :(得分:1)

好的,有一种更优雅的方式,但这个对我有用:

In [2]:

df = pd.DataFrame(data={"names": ["John ", "Jack"], "surnames": ["Andrews", " McAllister"]})

In [3]:

df

Out[3]:
    names   surnames
0   John    Andrews
1   Jack    McAllister

2 rows × 2 columns
In [9]:

df = df.apply(lambda x: x.str.strip())

In [10]:

df.loc[0, "names"]

Out[10]:

'John'