【问题标题】:How to remove rows of a dataframe based on values of a specific column [duplicate]如何根据特定列的值删除数据框的行[重复]
【发布时间】:2020-05-26 08:00:40
【问题描述】:

假设我有一个数据框:

       col1  col2  col3
0      8     7     5
1      6     2     17
2      3     1     21
3      4     3     9

我想删除 col3 值大于 10 且小于 20 的每一行并得到结果:

       col1  col2  col3
0      8     7     5
1      3     1     21
2      4     3     9

如何在 python 中编写代码?

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    只提取不符合上述条件的所有内容:

    df[~((df.col3 > 10) & (df.col3 < 20))]
    

    结果:

       col1  col2  col3
    0     8     7     5
    2     3     1    21
    3     4     3     9
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-23
      • 2021-12-06
      • 1970-01-01
      • 2022-11-17
      • 2016-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多