首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从dataframe中删除行,其中从第三列开始的每个值都是0

从dataframe中删除行,其中从第三列开始的每个值都是0
EN

Stack Overflow用户
提问于 2020-11-22 23:51:18
回答 1查看 28关注 0票数 0

我正在尝试删除从第三列开始值为0的行

我使用了下面的代码,它是有效的,但我觉得必须有一种更有效的方式来做到这一点,这是我的数据框架:

代码语言:javascript
复制
NRC_lexicon_wide = NRC_lexicon_wide[~((NRC_lexicon_wide['anger'] == 0) & (NRC_lexicon_wide['anticipation'] == 0) 
                                      & (NRC_lexicon_wide['disgust'] == 0) & (NRC_lexicon_wide['fear'] == 0) 
                                      & (NRC_lexicon_wide['negative'] == 0) & (NRC_lexicon_wide['positive'] == 0) 
                                      & (NRC_lexicon_wide['sadness'] == 0) & (NRC_lexicon_wide['surprise'] == 0)
                                      & (NRC_lexicon_wide['trust'] == 0))]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-23 19:06:13

当然,这样如何:

代码语言:javascript
复制
import pandas
import numpy


# Create a dataframe from a list of dicts will automatically find the column
df = pandas.DataFrame(pandas.DataFrame([{key: numpy.random.choice([0, 1, 2], p=[0.8, 0.15, 0.05]) for key in ["ColA", "ColB", "ColC", "ColD", "ColE", "ColF"]} for _ in range(50)]))

# Start from this column onwards
start_column = 3

# Get a boolean value for each cell, indicating if the value is larger than 0
larger_than_zero = df.loc[:, df.columns[start_column:]] > 0

# Get the rows for which any value in a cell is larger than 0
any_cell_larger_than_zero = larger_than_zero.any(axis=1)

# Select only the rows that have cells larger than 0
df = df.loc[any_cell_larger_than_zero]

# Or in a single line:
df = df.loc[(df.loc[:, df.columns[3:]] > 0).any(axis=1)]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64956192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档