pandas -在列中拆分文本并在行中搜索 [英] Pandas- Split text in column and search in rows
本文介绍了 pandas -在列中拆分文本并在行中搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此问题与以下历史记录有关:链接
This question is with this history: Link
这是json格式的表:
Here is a json format table:
ID Title
19 I am doing great
25 [Must fix problem] Stomach not well
31 [Not-so-urgent] Wash cloths
498 [VERY URGENT] Pay your rent
517 Landlord wants you to pay your rent tomorrow
918 Girlfriend wants to help you to pay rent if you take her out
1000 [Always reproducible issue] Room partner dont want to pay any rent, he is out of cash
我做到了
在: selected_row_title = df.loc [df ['id'] == 4] [标题"]
In: selected_row_title = df.loc[df['id'] == 4]["title"]
输出:
[VERY URGENT] Pay your rent
现在,通过使用Python Pandas,我试图将函数编写为:
Now, by using Python Pandas, I am trying to write a function as:
get_matching_rows(selected_row_title )
输出
ID 498 has pay your rent
ID 517 has pay your rent
ID 918 has pay rent
ID 1000 has pay rent
我一直在为此努力,我确实需要一些帮助,至少对如何实现这一目标提供了指导.感谢任何输入.
I have been tearing my hair out on this and I really need some help, atleast a guidance on how once can implement this. Appreciate any inputs.
推荐答案
我认为您可以使用 str.contains
:
I think you can use str.replace
with str.contains
:
s = "[VERY URGENT] Pay your rent"
#replace all [] in column title
tit = df.Title.str.replace(r'[\[\]]', '')
print (tit)
0 I am doing great
1 Must fix problem Stomach not well
2 Not-so-urgent Wash cloths
3 VERY URGENT Pay your rent
4 Landlord wants you to pay your rent tomorrow
5 Girlfriend wants to help you to pay rent if yo...
6 Always reproducible issue Room partner dont wa...
Name: Title, dtype: object
#search one of word of string s (logical or is |)
mask = tit.str.contains(s.replace(' ', '|'))
print (mask)
0 False
1 False
2 True
3 True
4 True
5 True
6 True
Name: Title, dtype: bool
#select all ID by condition
selected_row_title = df.loc[mask, 'ID']
print (selected_row_title)
2 31
3 498
4 517
5 918
6 1000
Name: ID, dtype: int64
这篇关于 pandas -在列中拆分文本并在行中搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文