python - Pandas 数据框排除特定范围内的行

标签 python pandas dataframe

我有一个 DataFrame df如下。我只是想排除特定列中的行,例如 Vader_Sentiment ,其值范围为 -0.1 到 0.1,并保留其余部分。

我已经尝试过df = [df['Vader_Sentiment'] < -0.1 & df['Vader_Sentiment] > 0.1]但似乎不起作用。

  Text        Vader_Sentiment       
   A               -0.010   
   B                0.206   
   C                0.003   
   D                -0.089  
   E                0.025   

最佳答案

您可以使用Series. Between():

df.loc[~df.Vader_Sentiment.between(-0.1, 0.1)]

  Text  Vader_Sentiment
1    B            0.206

三件事:

  • 波形符 (~) 运算符表示逆/补。
  • 确保您有数字数据。 df.dtypes 应显示 Vader_Sentiment 的 float ,而不是“object”
  • 您可以指定一个 inclusive 参数来说明您是否希望间隔关闭或打开

关于python - Pandas 数据框排除特定范围内的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47625793/

相关文章:

python - Windows 上的 PyGObject 和 cx_freeze

Python Pandas - 将多个电子表格中的特定选项卡合并为一个

python - MemoryError 将两个数据帧与 pandas 和 dasks 合并——我该怎么做?

python - Pandas 数据框创建为 2 系列的排列?

python - Bash 脚本到 python 脚本的转换

python - unpickle 自动训练模型时没有名为 'automl' 的模块

python - 如何在 Pandas 中将 bool 索引器与多索引结合起来?

python - 从多个字典创建数据框

python - 如何在 PyQt5 上使用 Vars,就像在 tkinter 上使用 IntVar() 或 StringVar() 一样?

python - Pandas - 根据其他列中的值将数据添加到列