如何删除pandas DataFrame中包含零的尾随行

dwthyt8l  于 2023-05-27  发布在  其他
关注(0)|答案(4)|浏览(160)

我有一个pandas dataframe,它有一个单列,以一些值为零结束,如下所示:

index value
0    4.0
1    34.0
2    -2.0
3    15.0
...    ...
96     0.0
97     45
98     0.0
99     0.0
100    0.0

我想去掉包含零值的尾随行,生成以下数据框:

index value
0    4.0
1    34.0
2    -2.0
3    15.0
...    ...
96     0.0
97     45

如何利用pandas的功能来实现这一点?
我知道我可以迭代地检查dataframe的最后一个值,如果它是零,就删除它,但我宁愿用一种利用pandas内置函数的方式来做,因为这样会快得多。

while df.iloc[-1,0] == 0:
    df.drop(df.tail(1).index,inplace=True)

EDIT:需要明确的是,dataframe可能包含也可能不包含其他零。但是,我只想去掉尾随的零,而其他零应该保持不变。我已经相应地编辑了示例。

yyhrrdl8

yyhrrdl81#

使用反向cummax的布尔索引:

out = df[df.loc[::-1, 'value'].ne(0).cummax()]

输出:

value
index       
0        4.0
1       34.0
2       -2.0
3       15.0
97      45.0

中间体:

value   mask
index              
0        4.0   True
1       34.0   True
2       -2.0   True
3       15.0   True
97      45.0   True
98       0.0  False
99       0.0  False
100      0.0  False

或者,如果您确定至少有一个非零值:

out = df.loc[:df.loc[::-1, 'value'].ne(0).idxmax()]
cfh9epnr

cfh9epnr2#

假设零值都堆叠在DataFrame的末尾:

# find the index of the last non-zero value
last_nonzero_index = df['value'].to_numpy().nonzero()[0][-1]

# create a new DataFrame with only the non-zero rows
new_df = df.iloc[:last_nonzero_index + 1]

否则,如果它们分散在整个DataFrame中:

# find index of non-zero values
nonzero_index = df['value'].to_numpy().nonzero()[0]

# create a new DataFrame with only the non-zero rows
new_df = df.iloc[nonzero_index]
xqnpmsa8

xqnpmsa83#

你可以通过广播来实现

df = df[(df != 0.0).any(axis=1)]
atmip9wb

atmip9wb4#

您可以将value列与0进行比较,并对布尔结果进行反向累积和。拖尾0将在累计之后保持0。

out = df[df.loc[::-1, 'value'].ne(0).cumsum()[::-1].ne(0)]
print(out)

    value
0     4.0
1    34.0
2    -2.0
3    15.0
4     0.0
97   45.0

相关问题