csv注释中的python关键字搜索
我正在尝试在csv文件中只在列注释中进行多关键字搜索。由于某些原因,当我尝试搜索时,我收到此错误消息“DataFrame”对象没有属性“description” 比如说csv注释中的python关键字搜索,python,pandas,csv,search,Python,Pandas,Csv,Search,我正在尝试在csv文件中只在列注释中进行多关键字搜索。由于某些原因,当我尝试搜索时,我收到此错误消息“DataFrame”对象没有属性“description” 比如说 table1.csv id_Acco, user_name, post_time comments 1543603, SameDavie , "2020/09/06" The car in the house 1543595, John
table1.csv
id_Acco, user_name, post_time comments
1543603, SameDavie , "2020/09/06" The car in the house
1543595, Johntim, "2020/09/11" You can filter the data
1558245, ACAtesdfgsf , "2020/09/19" if you’re looking at a ship
1558245, TDRtesdfgsf , "2020/09/19" you can filter the table to show
输出
id_Acco, user_name, post_time comments
1543603, SameDavie , "2020/09/06" The car in the house
1543595, Johntim, "2020/09/11" You can filter the data
1558245, TDRtesdfgsf , "2020/09/19" you can filter the table to show
代码
您可以使用下面的代码使用regex进行过滤 这里,我们使用r字符串来包含正则表达式元字符 我们使用
\b
包含4个目标单词,这样它将只匹配整个单词而不是部分字符串。例如,卡门
与汽车
不匹配,汤匙
与表格
不匹配。如果要匹配部分字符串,可以删除上面正则表达式中的\b
对
你可以看看这个匹配的演示
结果:
print(df)
id_Acco, user_name, post_time comments
0 1543603, SameDavie , "2020/09/06" The car in the house
1 1543595, Johntim, "2020/09/11" You can filter the data
3 1558245, TDRtesdfgsf , "2020/09/19" you can filter the table to show
太好了,你选择了适合你的答案!请考虑一下(现在你有15个名声,应该能够投票)。谢谢
df = df.loc[df.comments.str.contains(r'\b(?:house|filter|table|car)\b')]
print(df)
id_Acco, user_name, post_time comments
0 1543603, SameDavie , "2020/09/06" The car in the house
1 1543595, Johntim, "2020/09/11" You can filter the data
3 1558245, TDRtesdfgsf , "2020/09/19" you can filter the table to show