DataFrame constructor not properly called error

0 votes


I am new to using the Python language and am co facing problem in creating the Dataframe in the format of key and value i.e.

data = [{'key':'\[GlobalProgramSizeInThousands\]','value':'1000'},]


Here is my code:

columnsss = ['key','value']; 
query = "select * from bparst_tags where tag_type = 1 "; 
result = database.cursor(db.cursors.DictCursor); 
result.execute(query); 
result_set = result.fetchall(); 
data = "["; 
for row in result_set: 
`row["tag_expression"]`) 
      data += "{'value': %s , 'key': %s }," % ( `row["tag_expression"]`, `row["tag_name"]` ) data += "]"
; 
df = DataFrame(data , columns=columnsss);

But when I pass the data in DataFrame it shows me

pandas.core.common.PandasError: DataFrame constructor not properly called!

while if I print the data and assign the same value to the data variable then it works.

Feb 22, 2022 in Python by Rahul
• 9,680 points
2,476 views

1 answer to this question.

0 votes

You are providing a string representation of a dict to the DataFrame constructor, and not a dict itself. So this is the reason you get that error.

So if you want to use your code, you could do:

df = DataFrame(eval(data))

But better would be to not create the string in the first place, but directly putting it in a dict. Something roughly like:

data = [] 
for row in result_set: 
    data.append({'value': row["tag_expression"], 'key': row["tag_name"]})

But probably even this is not needed, as depending on what is exactly in your result_set you could probably provide this directly to a DataFrame: DataFrame(result_set) or even use the pandas read_sql_query function to do this for you 

answered Feb 22, 2022 by Aditya
• 7,680 points

Related Questions In Python

0 votes
3 answers

Python Pandas: ValueError: DataFrame constructor not properly called!

The syntax is not right. The right ...READ MORE

answered Mar 28, 2019 in Python by Ritu
85,725 views
0 votes
1 answer
0 votes
1 answer

Python argparse error "NameError: name 'file' is not defined"

The right datatype for destination in argpasrse module ...READ MORE

answered Nov 28, 2018 in Python by Omkar
• 69,220 points
13,779 views
0 votes
3 answers

Python error "NameError: name 'sr' is not defined"

NameError: name 'xx' is not defined Python knows ...READ MORE

answered Mar 19, 2020 in Python by rahul
• 360 points
42,660 views
0 votes
1 answer

How to print a message or the error if a file is not found?

To print the message that file is not ...READ MORE

answered Jan 2, 2019 in Python by Omkar
• 69,220 points
2,901 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,463 views
0 votes
1 answer
0 votes
1 answer

'str' object does not support item assignment

In Python, strings are not very mutable ...READ MORE

answered Feb 22, 2022 in Python by Aditya
• 7,680 points
1,363 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP