close

[Solved] DataFrame constructor not properly called!

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error DataFrame constructor not properly called! in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How DataFrame constructor not properly called! Error Occurs?

Today I get the following error DataFrame constructor not properly called! in python.

How To Solve DataFrame constructor not properly called! Error ?

  1. How To Solve DataFrame constructor not properly called! Error ?

    To Solve DataFrame constructor not properly called! Error 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.

  2. DataFrame constructor not properly called!

    To Solve DataFrame constructor not properly called! Error 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.

Solution 1

Just ran into the same error, but the above answer could not help me.

My code worked fine on my computer which was like this:

test_dict = {'x': '123', 'y': '456', 'z': '456'}
df=pd.DataFrame(test_dict.items(),columns=['col1','col2'])

However, it did not work on another platform. It gave me the same error as mentioned in the original question. I tried below code by simply adding the list() around the dictionary items, and it worked smoothly after:

df=pd.DataFrame(list(test_dict.items()),columns=['col1','col2'])

Hopefully, this answer can help whoever ran into a similar situation like me.

Solution 2

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 use the pandas read_sql_query function to do this for you

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read