【问题标题】:Drop ALL rows in pandas until a specific string is found then stop dropping删除 pandas 中的所有行,直到找到特定字符串然后停止删除
【发布时间】:2022-07-08 21:34:42
【问题描述】:

我有一个数据框,我想删除一行,直到找到特定的字符串。

我尝试了几件事,但似乎没有任何效果。这是我尝试过的:

df[((df.Plate != 'Group Summaries'))]
df.loc[: df[(df['Plate:'] == 'Group Summaries')].index[0], :]
df[(df['Plate:'] == "Group Summaries").idxmax():]

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    你可以使用:

    df[df['Plate'].eq('Group Summaries').cummax()]
    

    它是如何工作的:

    • 在“Plate”列中将带有“Group Summary”的行标记为 True
    • 使用cummax 将 True 转发到所有下一行
    • 使用布尔索引来选择行

    【讨论】:

      猜你喜欢
      • 2022-10-12
      • 1970-01-01
      • 1970-01-01
      • 2018-12-08
      • 2020-06-15
      • 1970-01-01
      • 2022-01-19
      • 2021-12-03
      相关资源
      最近更新 更多