首页 > 解决方案 > 如何比较数据框中的确切字符串?

问题描述

我正在尝试使用数据框修改用 Python 编写的前同事的代码。

我们想从数据框中删除所有df_msc不包含drives_tup“已安装”列中的字符串之一的行。他使用字符串修饰符startswithand对其进行了修剪endswith,但这仍然会通过不需要的行。有没有办法做类似的事情string.isequalto(drives_tup)

我不擅长 Python,所以我不想过多地改变格式,因为我担心它会破坏代码的其他部分......

      # Take only those drives where they start or end with one of the drives_tup values
      drives_tup = ("/usr/asm", "/data/reserved", "/var", "/", "/data/diagnostics")
      df_msc = df_msc.iloc[:, 1:-1].dropna(how='all')
      df_msc = df_msc.loc[df_msc['Mounted'].str.startswith(drives_tup)]
      df_msc = df_msc.loc[df_msc['Mounted'].str.endswith(drives_tup)]

标签: pythondataframe

解决方案


df_msc = df_msc.loc[df_msc['Mounted'].isin(drives_tup)]

推荐阅读