【发布时间】:2021-03-10 10:16:17
【问题描述】:
我在尝试将 dtype 表示形式转换为 dict 时遇到问题。 实际上,我通过 API 发送这个字符串,以便在 read_csv 函数中使用它:
MyString='{"dept": str}' # This is received from an API
pd.read_csv(file, dtype=MyString)
--> data type '{"dept": str}' not understood: TypeError
# Then, I tried:
pd.read_csv(file, dtype=dict(MyString))
--> ValueError: dictionary update sequence element #0 has length 1; 2 is required
# In a desperate move, I did:
pd.read_csv(file, dtype=ast.literal_eval(MyString))
--> ValueError: malformed node or string: <_ast.Name object at 0x7f3527139128>
我缺少什么,请问我该如何实现?来自熊猫文档:
dtype : Type name or dict of column -> type, optional
Data type for data or columns
【问题讨论】:
标签: python python-3.x pandas dictionary