I am trying to create a dataframe with Python, which works fine with the following command:
df_test2 = DataFrame(index = idx, data=(["-54350","2016-06-25T10:29:57.340Z","2016-06-25T10:29:57.340Z"]))
but, when I try to get the data from a variable instead of hard-coding it into the data argument; eg. :
r6 = ["-54350", "2016-06-25T10:29:57.340Z", "2016-06-25T10:29:57.340Z"] df_test2 = DataFrame(index = idx, data=(r6))
I expect this is the same and it should work? But I get:
ValueError: DataFrame constructor not properly called!
Advertisement
Answer
Reason for the error:
It seems a string representation isn’t satisfying enough for the DataFrame constructor
Fix/Solutions:
import ast # convert the string representation to a dict dict = ast.literal_eval(r6) # and use it as the input df_test2 = DataFrame(index = idx, data=(dict))
which will solve the error.