【发布时间】:2021-09-15 00:35:31
【问题描述】:
我有一列字符串(句子)和一列逗号分隔的字符串列表,如下所示:
df = pd.DataFrame({ 'text':['the weather is nice though', 'How are you today','the beautiful girl and the nice boy'],
'pos':[['DET', 'NOUN', 'VERB','ADJ', 'ADV'],['QUA', 'VERB', 'PRON', 'ADV'], ['DET', 'ADJ', 'NOUN','CON','DET', 'ADJ', 'NOUN' ]]})
我想以某种方式比较列,并创建第三列,如果“pos”列包含值“ADJ”,我会在“text”列中找到它的对应值(在本例中第一行我有'nice')并以字典的形式返回它的索引。所以这就是第三列的样子;
third_column:
1 {'nice' : 3}
2 {}
3 {'beautiful':1, 'nice':6}
到目前为止,我已经尝试了以下方法:
df['Third_column']= ' '
df['liststring'] = [' '.join(map(str, l)) for l in df['pos']]
df.loc[df['liststring'].str.contains('ADJ'),'text']
但不知道如何继续获取确切的单词和索引
【问题讨论】:
标签: python pandas string list indexing