[英]creating dataframe from csv file having lists as entries in one of the columns
I have a csv
file which looks like this - 我有一个看起来像这样的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'}]
When I import the csv
as dataframe
, the lists
in genres
column are loaded as strings. 当我将csv
导入为dataframe
, genres
列中的lists
将作为字符串加载。 For example - "[{'id': 35, 'name': 'Comedy'}]"
例如- "[{'id': 35, 'name': 'Comedy'}]"
How do I load the lists
without the quotes
? 如何加载不带quotes
的lists
?
Use: 采用:
import ast, json
df['genres'] = df['genres'].apply(ast.literal_eval)
Or: 要么:
df['genres'] = df['genres'].apply(json.loads)
Also using strip()+split(): 同样使用strip()+ split():
df['genres']= [x.strip("[]").split(',') for x in df['genres']]
or, 要么,
df['genres']= df['genres'].apply(lambda x: x.strip("[]").split(','))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.