Answer a question

I have a function that pulls tables from our a table in our SQL server into a dataframe in Python, but it forces all the column headers to be lower case. The code is as follows:

connection = pypyodbc.connect('Driver={SQL Server};'
                              'Server='   + server   + ';'
                              'Database=' + database + ';'
                              'uid='      + username + ';'
                              'pwd='      + password + ';')
query = 'SELECT * FROM ' + tableName

#set dict value to dataframe imported from SQL
tableDict[tableName] = pd.read_sql(query, connection)

The headers in SQL are for example: pmiManufacturingHeadline_Level It shows up in my pandas dataframe as: pmimanufacturingheadline_level

Anyone have an idea how to make pandas.read_sql keep the original capitalization?

Answers

I think PyPyODBC does it for you:

Here what i found in the source code of PyPyODBC ver. 1.3.3 lines: 28-29:

version = '1.3.3'
lowercase=True

and lines 1771-1772:

        if lowercase:
            col_name = col_name.lower()

so you can change the behaviour if you want:

import pypyodbc
pypyodbc.lowercase = False  # force the ODBC driver to use case-sensitive column names
Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐