[英]ValueError: DataFrame constructor not properly called
I am trying to create a dataframe with Python, which works fine with the following command:我正在尝试使用 Python 创建一个数据框,它可以使用以下命令正常工作:
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!
Reason for the error:错误原因:
It seems a string representation isn't satisfying enough for the DataFrame constructor似乎字符串表示不足以满足 DataFrame 构造函数
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.这将解决错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.