我有一个 csv
文件,如下所示 -
id genres
1 [{'id': 35, 'name': 'Comedy'}]
2 [{'id': 35, 'name': 'Comedy'}, {'id': 18, 'name': 'Drama'}, {'id': 10751, 'name': 'Family'}, {'id': 10749, 'name': 'Romance'}]
3 [1,2,3]
4 [{'id':31, 'name':'Comedy'}]
当我将 csv
导入为 dataframe
时,genres
列中的 lists
将作为字符串加载。例如 - “[{'id': 35, 'name': 'Comedy'}]”
如何加载不带引号
的列表
?
最佳答案
用途:
import ast, json
df['genres'] = df['genres'].apply(ast.literal_eval)
或者:
df['genres'] = df['genres'].apply(json.loads)
关于python - 从 csv 文件创建数据框,其中列表作为其中一列中的条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55454575/