[Python] Pyodbc returning multiple sql server query columns into one column

Discussão em 'Python' iniciado por Stack, Maio 30, 2025.

  1. Stack

    Stack Membro Participativo

    Using pyodbc, I'm trying to return the results of a simple query from a sql server table into a dataframe. When the results are returned and I'm trying to pull in the column names, I keep getting an error

    ValueError: Shape of passed values is (18,1) indices imply (18,2)


    I've run similar code in terradata before without an issue so I'm not sure why my code would only return a single row, but recognize two columns. Here's an example of what I'm trying to run

    conn = pyodbc.connect(cxn) #cxn is just my connection string and no issues here
    cursor = conn.cursor()
    query = cursor.execute("Select [FirstName], [LastName] from [People] where [ID] > 500")
    peeps = pd.DataFrame(query, columns = [i[0] for i in cursor.description])


    When I remove the "columns" piece of code from the peeps variable, I return the values in a single column similar to something like this

    0
    0 [John, Smith]


    Any idea why this would be happening? If additional details are needed I'm happy to provide.

    Continue reading...

Compartilhe esta Página