Skip to main content
 首页 » 编程设计

python之订购数据框并按词搜索

2024年10月01日6rubylouvre

当单词 Sony 出现在 csv 行中时,我正在使用此代码提取一些评论。现在我想知道如何按列 id 的顺序对搜索进行排序。

这是csv结构

id review 
#1 Some text ... 
#2 Some text ... 

df = pd.read_csv('./file.csv', delimiter= '\t', lineterminator="\n") 
print df[df['review\r'].str.contains("Sony")] 

请您参考如下方法:

我想你需要sort_values :

df[df['review\r'].str.contains("Sony")].sort_values('id')