如何将标头添加到从数据库检索的数据框?

时间:2019-08-14 15:36:04

标签: python pandas dataframe header pyodbc

我能够通过pyodbc连接到SSMS来获取数据。但是,数据没有标题。我想将这些标头添加为数据框的一部分

import pandas as pd
import pyodbc
conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=PMIV270\LION;'
                      'Database=Warehouse;'
                      'Trusted_Connection=yes;')

cursor = conn.cursor()
cursor.execute('SELECT TOP 10 [StreetAddressLine1],[City],[ZipCode],[Latitude],[Longitude] FROM Dim.Vet')
for row in cursor:
    print((row))

('21236 Birchwood Loop', 'Chugiak', '99567', None, None)
('1731 Bragaw St', 'Anchorage', '99508', None, None)
('300 E Fireweed Ln', 'Anchorage', '99503', Decimal('61.1980018615'), Decimal('-149.8779144287'))
('4360 Snider Dr', 'Wasilla', '99654', None, None)
('1921 W Dimond Blvd # 108', 'Anchorage', '99515', Decimal('61.1379394531'), Decimal('-149.9158020019'))
('2702 Peger Rd', 'Fairbanks', '99709', Decimal('64.8237533569'), Decimal('-147.7786560058'))
('1651 College Rd', 'Fairbanks', '99709', Decimal('64.8625717163'), Decimal('-147.7543182373'))
('898 Ballaine Rd', 'Fairbanks', '99709', Decimal('64.8984527587'), Decimal('-147.8298797607'))
('23819 Immelman Circle', 'Chugiak', '99567', Decimal('61.4179687500'), Decimal('-149.4366149902'))

input_file_path = "cursor"

output_file_path = "output"  # appends "####.csv" to the file name when it writes the file.

cursor.columns = ['ADDRESS','STATE','ZIP_CODE','Latitude','Longtitude']
Traceback (most recent call last):

  File "<ipython-input-21-2ea83ede543d>", line 1, in <module>
    cursor.columns = ['ADDRESS','STATE','ZIP_CODE','Latitude','Longtitude']

AttributeError: 'pyodbc.Cursor' object attribute 'columns' is read-only

我为什么收到这个

0 个答案:

没有答案