熊猫新手,抱歉,如果解决方案很明显。熊猫群大熊猫字典
我有一个数据帧(见下文)与不同的电影场景,对于电影中的场景
import pandas as pd
data = [{'movie' : 'movie_X', 'scene' : '1', 'environment' : 'home'},
{'movie' : 'movie_X', 'scene' : '2', 'environment' : 'car'},
{'movie' : 'movie_X', 'scene' : '3', 'environment' : 'home'},
{'movie' : 'movie_Y', 'scene' : '1', 'environment' : 'home'},
{'movie' : 'movie_Y', 'scene' : '2', 'environment' : 'office'},
{'movie' : 'movie_Z', 'scene' : '1', 'environment' : 'boat'},
{'movie' : 'movie_Z', 'scene' : '2', 'environment' : 'beach'},
{'movie' : 'movie_Z', 'scene' : '3', 'environment' : 'home' }]
myDF = pd.DataFrame(data)
环境。在这种情况下,电影有多个流派,他们属于哪个。我有一本字典(下),说明该类型属于
genreDict = {'movie_X' : ['romance', 'action'],
'movie_Y' : ['comedy', 'romance', 'action'],
'movie_Z' : ['horror', 'thriller', 'romance']}
我想是myDF组通过这本字典每部电影,特别是能够告诉的次数特定的环境特定类型止跌回升(例如,在类型恐怖中,'船'被计数一次,'海滩'被计数一次,'家'被计数一次)。什么是最好的和最有效的方式去做这件事?我试图映射字典数据框,然后由列表分组:
myDF['genres'] = myDF['movie'].map(genreDict)
将返回:
movie scene environment genres
0 movie_X 1 home [romance, action]
1 movie_X 2 car [romance, action]
2 movie_X 3 home [romance, action]
3 movie_Y 1 home [comedy, romance, action]
4 movie_Y 2 office [comedy, romance, action]
5 movie_Z 1 boat [horror, thriller, romance]
6 movie_Z 2 beach [horror, thriller, romance]
7 movie_Z 3 home [horror, thriller, romance]
但是,我得到了一个错误说列表是unhashable。希望你们都可以帮忙:)
你可以发表你想要的数据集? – MaxU