
上QQ阅读APP看书,第一时间看更新
138 使用contains()在指定列中筛选文本
此案例主要通过在字符串的contains()函数的参数中设置筛选条件,实现在指定列中筛选符合条件的数据。当在Jupyter Notebook中运行此案例代码之后,将在DataFrame中筛选学校名称包含“学院”的数据,效果分别如图138-1和图138-2所示。

图138-1

图138-2
主要代码如下。

在上面这段代码中,df[df['学校名称'].str.contains('学院')]表示在df中筛选学校名称包含“学院”的数据。
此案例的主要源文件是MyCode\H804\H804.ipynb。