DataFrame constructor not properly called!

Hello,



While using the below mentioned code:



>>> my_portfolio = """{

    "stock" : ["abc", "def", "ghi", "jkl"],

    "qunatity" : [100, 200, 300, 400],

    "buy_price : [20, 30, 40, 50] }"""

    

>>> my_portfolio_frame = pd.DataFrame (my_portfolio)



I'm getting this error-



ValueError: DataFrame constructor not properly called!



Can anyone help in explaining what and why is this error coming?





Thanks

 

my_portfolio = """{
    "stock" : ["abc", "def", "ghi", "jkl"],
    "qunatity" : [100, 200, 300, 400],
    "buy_price : [20, 30, 40, 50] }"""

In the above you have used 3 qoutation marks before and after the brackets, which have turned everything written within those marks to a comment. Python doesn't consider comments when executing the code. Try this instead:

my_portfolio = {
    "stock" : ["abc", "def", "ghi", "jkl"],
    "qunatity" : [100, 200, 300, 400],
    "buy_price : [20, 30, 40, 50] }

my_portfolio_frame = pd.DataFrame (my_portfolio)

This should fix the problem you are facing.

Thanks Abinash! It helped.

It seems a string representation isn't satisfying enough for the DataFrame constructor. This means that you are providing a string representation of a dict to DataFrame constructor, and not a dict itself. So this is the reason you get that error. Just simply adding the list() around the dictionary items,:
 

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


which will solve the error.