Selamola Tloubatla
Selamola Tloubatla

Reputation: 38

How does my output show real column headers INSTEAD OF AUTO INCREMENT NUMBERS

How does my output show real column headers instead of auto increment numbers.

mydb = mysql.connector.connect(
    host="***************",
    user="****",
    password="****")

    mycursor = mydb.cursor()

    query = "select * from odk_prod.individual_report_core where PROCESSED_BY_MIRTH = 0 "
    mycursor.execute(query)
    myresult = mycursor.fetchall()

    df = pd.DataFrame(myresult)

    print(df)

Upvotes: 0

Views: 212

Answers (3)

Serial Lazer
Serial Lazer

Reputation: 1669

You need to create the cursor so it can return the dictionary object instead:

mycursor = mydb.cursor(dictionary=True)

Upvotes: 1

Mayank Porwal
Mayank Porwal

Reputation: 34086

Create a list of all columns which you want df to have. Example:

cols = ['A', 'B', 'C', 'D']

Then do:

df.columns = cols

Upvotes: 1

orlp
orlp

Reputation: 117771

You can pass the column names explicitly:

df = pd.DataFrame(cursor.fetchall(), columns=[d[0] for d in cursor.description])

Upvotes: 1

Related Questions