简体   繁体   中英

delete CSV rows with conditional in python

I have a csv file with the following:

storeNumber, sale1, sale2
1, 1, 1
2, 0, 0
3, 1, 0
4, 0, 1
...
25, 0, 0
26, 1, 0
27, 0, 1
28, 0,0

I need to delete rows with sale1 and sale2 that are equal to 0.

I have the following code setup:

import pandas as pd
df = pd.read_csv('sales.csv', index_col=0)

df_new = df[df.sale1 != 0] and df[df.sale2 != 0]

print(df_new)

the code works if I will only delete one of each column that has 0 value.

df_new = df[df.sale1 != 0]

or

df_new = df[df.sale2 != 0]

However, when put the code above with the "and", I get an error that says:

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

what is the right code for deleting rows that have 0 value for both sale1 and sale2?

To operator you need to use to combine the two logical conditions is & instead of and . This is explained in detail here . So, what you need is:

df_new = df[(df.sale1 != 0) & (df[df.sale2 != 0)]  

Notice that both conditions must be in parentheses since & binds stronger than != .

Another way of writing this would be to keep only rows where any of the two columns is not equal to zero.

df.loc[df[['KC_1','KC_2']].ne(0).any(axis=1)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM