python - Pandas-在列中拆分文本并在行中搜索

标签 python pandas

这个问题有这样的历史:Link

这是一个json格式表:

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]["标题"]

输出:

[VERY URGENT] Pay your rent

现在,通过使用 Python Pandas,我尝试编写一个函数:

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

我一直在为此绞尽脑汁,我真的需要一些帮助,至少需要一个关于如何实现这一点的指导。感谢任何意见。

最佳答案

我认为你可以使用str.replacestr.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

关于python - Pandas-在列中拆分文本并在行中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37411300/

相关文章:

python - 不能在 64 位架构的 Python 中使用 128 位 float

python - 如何在mysql命令行中传递密码

python - 在 Python 中读取 PowerPoint 表格?

python - 按自定义日期对数据框进行分组

python - 基于索引的输出行

Python Pandas : Convert timedelta Value From Subtracting Two Dates Into Integer Datatype (AttributeError)

python - 如何读取可以在 python 中保存为 ansi 或 unicode 的文件?

python - 使用 pandas,如果另一列不为空,如何使用 dataframe 查找列值?

regex - Python 3 Pandas 通过正则表达式和通配符过滤数据框

python - 将字典的字典转换为 DataFrame Python