我正在尝试创建列表元素值到索引的映射。例如,给定如下数据帧:
>>> book_df
name genre
0 Harry Potter ["fantasy", "young adult"]
1 Lord of the Rings ["fantasy", "adventure", "classics"]
2 I, Robot ["science fiction", "classics"]
3 Animal Farm ["classics", "fantasy"]
4 A Monster Calls ["fantasy", "young adult"]
我想生成一个dict,它将流派映射到该流派下的电影列表。你知道吗
所以,我想要得到的是这样的东西:
>>> genre_to_book_map
{
"fantasy": ["Harry Potter", "Lord of the Rings", "Animal Farm", "A Monster Calls"],
"young adult": ["Harry Potter", "A Monster Calls"],
"classics": ["Lord of the Rings", "I, Robot", "Animal Farm"],
"science fiction": ["I, Robot"],
"adventure": ["Lord of the Rings"]
}
我以一种相当冗长的方式完成了这个任务,将列表分解,然后用它创建一个字典(基于Pandas column of lists, create a row for each list element和Pandas groupby two columns then get dict for values),如下所示:
exploded_genres = pd.DataFrame({
"name" :np.repeat(book_df["name"].values, book_df["genres"].str.len())
}).assign(**{"genres":np.concatenate(book_df["genres"].values)})
genre_to_name_map = exploded_genres.groupby("genres")["name"].apply(lambda x: x.tolist())
但我想知道是否有更有效的方法来做这件事,因为这似乎是一件相对简单的事情
使用旧的良好
collections.defaultdict
对象:您需要将列表融合到各个流派中,然后按流派分组并输出到字典中。你知道吗
因为
0.25
,所以可以使用explode
来展开列表。你知道吗相关问题 更多 >
编程相关推荐