RAJAWAT
RAJAWAT

Reputation: 41

converting a list of dictionaries to a data frame

I referred to this link Convert list of dictionaries to Dataframe but i get error of "DataFrame constructor not properly called"

I tried DataFrame.from_dict but the error is same. The dictionary is the result from a database query. fetched from cursor.fetchall().

Thanks for help.

code: 
import MySQLdb
import pandas as pd
def query_run(sql):
    db=MySQLdb.connect("127.0.0.1","user","password","db")
    cursor = db.cursor(MySQLdb.cursors.DictCursor)
    try:
        cursor.execute(sql)
        data = cursor.fetchallDict()
        db.close()
    return data
query = "select * from table"
data = query_run(query)
dataframe = pd.DataFrame(data) #tried this as well as
dataframe = pd.DataFrame.from_dict(data) #this as well

i have some processing to do on dict data that's why i'm not fetching the rows as dataframes from database

Upvotes: 0

Views: 643

Answers (1)

RAJAWAT
RAJAWAT

Reputation: 41

the problem got solved from using DataFrame.from_records(data)

Upvotes: 2

Related Questions