我有以下 Dataframe
df1 = pd.DataFrame({'sentence': ['A', "A", "A", "A", 'A', 'B', "B", 'B'], 'entity': ['Stay home', "Stay home", "WAY", "WAY", "Stay home", 'Go outside', "Go outside", "purpose"], 'token' : ['Severe weather', "raining", "smt", "SMT0", "Windy", 'Sunny', "Good weather", "smt"]
})
sentence entity token
0 A Stay home Severe weather
1 A Stay home raining
2 A Way smt
3 A Way SMT0
4 A Stay home Windy
5 B Go outside Sunny
6 B Go outside Good weather
7 B Purpose smt
当entity
列中存在Way
和Purpose
时,我想对sentences
的值执行group by
运算并创建新的columns
预期成果:
sentence entity token Way Purpose
0 A Stay home Severe weather, raining, Windy smt, SMTO Nan
1 B Go outside Sunny, Good weather Nan smt
1条答案
按热度按时间dauxcl2d1#
在
boolean indexing
中按Series.isin
筛选不匹配的行,其中~
用于反转掩码,聚合join
并使用DataFrame.join
筛选匹配列表中的行,其中DataFrame.pivot_table
: