Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在SQLAlchemy中从SQL查询中删除行号列_Python_Excel_Sqlalchemy_Pyodbc - Fatal编程技术网

Python 在SQLAlchemy中从SQL查询中删除行号列

Python 在SQLAlchemy中从SQL查询中删除行号列,python,excel,sqlalchemy,pyodbc,Python,Excel,Sqlalchemy,Pyodbc,每当我查询我的sqldb以创建一个df时,我总是会得到一列行号。我试过: df.drop(df.columns[[0]], axis=1, inplace=True) 但它会忽略该列,并删除我在查询中请求的第一列数据。所以我留下了一列从0开始一直向下的行号。无论我使用的是SQLAlchemy还是Pyodbc,都会发生这种情况 我的数据框看起来像 | action| employee_number|part_number|time_stamp 0 |Add/Sub| 001841

每当我查询我的sqldb以创建一个df时,我总是会得到一列行号。我试过:

df.drop(df.columns[[0]], axis=1, inplace=True)
但它会忽略该列,并删除我在查询中请求的第一列数据。所以我留下了一列从0开始一直向下的行号。无论我使用的是SQLAlchemy还是Pyodbc,都会发生这种情况

我的数据框看起来像

   | action| employee_number|part_number|time_stamp
0  |Add/Sub| 001841         |F151519FGL |2015-10-01
1  |Remove | 001997         |P088001DFL |2015-10-01
2  |Add/Sub| 001243         |-F151517DDL|2015-10-01
........................................................
50 |Add/Sub| 001458         |-1A0021049 |2015-10-01
我想把0和后面的所有数字减到50+(特别是因为这些都是写在Excel中的,所以有额外的行号似乎是多余的)

作为参考,我的完整代码如下:

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

##Needed in order to get SQL query properly formatted ***SHOULD BE CHANGED LATER****
pd.core.format.header_style = None 
pd.core.format.number_format = None 
#######################################################################################

##Takes sqlalchemy query and a list of columns, returns a dataframe.
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])
############################################################################################

#####Creates Engine, Named Session(Currently Not Scoped), Declarative Base############################  
engine = create_engine('mssql+pyodbc://u:pass@Server/TableName?driver=SQL Server', echo=False)
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()



###########Class Name, __tablename__ is equal to table's name in db but we are specifying all column names & formats with at least one primary key######################
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(100))    

###Creating a variable with session query for specific Class Name  with filter (not filter_by which allows for extra operators)##############
newvarv = session.query(Tranv).filter_by(employee_number='001841').filter_by(time_stamp='2015-10-01 10:49:53.230')

###Uses data_frame function with input of session query variable name, and extra within [] the specified class must be included###############
dfx = data_frame(newvarv, [c.name for c in Tranv.__table__.columns])
##dfx.drop(dfx.columns[[0]], axis=1, inplace=True)
###Where I'm writing file to
writer = pd.ExcelWriter('C:\\Users\\grice\\Desktop\\Auto_Scrap_Report\\testy.xlsx')
###Formatting of any date times
writer.date_format = None
writer.datetime_format = None

###Actually writing the data to the .xlsx and saving
dfx.to_excel(writer, sheet_name='Sheet1')
writer.save()

它不是一列,而是一个
索引
,每个
数据帧
必须有一个(因为它必须有
)。但是,当您导出到excel时,您可以指示pandas不要导出索引:
dfx.to_excel(…,index=False)
谢谢您,我在前面的文档中读到了这一点,但在索引和我遇到的问题之间有一个完全的心理断开。