我有2个数据框.
我需要根据另一数据框中的值读取数据
话:
words = pd.DataFrame()
words['no'] = [1,2,3,4,5,6,7,8,9]
words['word'] = ['cat', 'in', 'hat', 'the', 'dog', 'in', 'love', '!', '<3']
words
句子:
sentences = pd.DataFrame()
sentences['no'] =[1,2,3]
sentences['start'] = [1, 4, 6]
sentences['stop'] = [3, 5, 9]
sentences
cat in hat
***
the dog
***
in love ! <3
但是我无法通过此步骤,我尝试运行以下代码:
对于x的情感:
print(words [‘word’] [words [‘no’].between(句子[‘start’],句子[‘stop’],包括= True)
但返回此错误
File "<ipython-input-16-ae3f5333be66>", line 3
print(words['word'][words['no'].between(sentences['start'], sentences['stop'], inclusive = True)
^
SyntaxError: unexpected EOF while parsing
解决方法:
res=pd.DataFrame()
res['s']=sentences.apply(lambda x: ' '.join(words.iloc[(x['start']-1):(x['stop'])]['word']),axis=1)
res.to_csv('a.txt',index=False,header=False,line_terminator='\n***\n')