tamo007
tamo007

Reputation: 285

pandas: remove row where value is equal to zero

i want if the the result of this command is >1 item then delete row with the zero.

list_count = df.groupby(['id_client', 'date'])['count'].apply(list).reset_index()

example of result the code above

My original df should have:

908     01/2020     0
908     01/2020     35
907     01/2020     0
907     01/2020     37
909     01/2020     50
910     01/2020      0

result of the above code line

    908     01/2020     [0, 35] 
    907     01/2020     [0, 37]
    909     01/2020      50
    910     01/2020      0

expected output

908     01/2020     35
907     01/2020     37
909     01/2020     50
910     01/2020      0

Upvotes: 3

Views: 246

Answers (1)

mozway
mozway

Reputation: 262519

You can compute two masks and slice:

# values where group has only 1 element
m1 = df.groupby(['id_client', 'date'])['count'].transform('size').eq(1)
# values ≠ 0
m2 = df['count'].ne(0)
# keep values that have either criterion
df[m1|m2]

output:

   id_client     date  count
1        908  01/2020     35
3        907  01/2020     37
4        909  01/2020     50
5        910  01/2020      0

Upvotes: 1

Related Questions