当我试图从数据集的一列中提取信息时,我遇到了一个问题
列示例:
styles | [{"id" 28日,"name":"Action"},{"id" 12,"name":"Adventure"},{"id" 14日"name":"Fantasy"},{"id" 878年,"name":"科学Fiction"}] |
---|
[{"id" 12,"name":"Adventure"},{"id" 14日"name":"Fantasy"},{"id" 28日,"name":"Action"}] |
try this:
def extract_all_genre_names(x):
try:
result = [d['name'] for d in eval(x)]
except:
result = x
return result
out = df['genres'].apply(extract_all_genre_names)
print(out)