微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python-根据不同数据框中的条件从数据框中读取日期

我有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')

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐