【问题标题】:Pandas drop rows when all columns in a subset of columns meet a condition [duplicate]当列子集中的所有列都满足条件时,熊猫会删除行[重复]
【发布时间】:2021-08-26 22:54:56
【问题描述】:

有一个列列表,如果我要删除的行全部为零。随着计划数量的变化动态生成列表。 dmt_clean 是数据框。

dmt_clean.head()

OptionNumber    TradeCategory   4015-7  4017-7  4019-7  4025-7  4030-6
0   1   Doors - Exterior    280.226822  280.226822  280.226822  280.226822  280.226822
1   1   Hardware    175.99  175.54  184.25  226.49  267.82
2   1   Trim - Interior 1818.783178 1904.233178 2252.523178 2589.283178 3323.953178
3   70000   Trim - Interior 0   0   0   0   0
4   70002   Trim - Interior 0   0   0   0   0

# get list of only plan number columns
l_plan_cols = []
for c in dmt_clean.columns:
    if re.search('\d{4}-\d{1}', c):
        l_plan_cols.append(c)

dmt_clean[l_plan_cols] == 0

    4015-7  4017-7  4019-7  4025-7  4030-6
0   False   False   False   False   False
1   False   False   False   False   False
2   False   False   False   False   False
3   True    True    True    True    True
4   True    True    True    True    True
5   True    True    False   True    True
6   False   False   False   True    False

【问题讨论】:

  • df[~df.all(axis=1)]
  • 这个answer来自副本。

标签: python pandas


【解决方案1】:
df = df.dropna(axis=0, how='all')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多