0
df = pd.DataFrame({
'Number' : ['IN-60','IN-60','AMD-60','AMD-60','IN-60','AMD-60','AMD-62','AMD-62']
'Value':['0','10','51','50','20','0','0','0']

})

Input :-

Number   Value
IN-60     0       
IN-60     10      
AMD-60    51      
AMD-60    50      
IN-60     20      
AMD-60    0      
AMD-62    0       
AMD-62    0       

I want drops rows where Number are same and value is 0 and if 2 more same number entry with zero then i want one number in dataframe.
I want to Output like :

Output:-

Number   Value   
IN-60     10      
IN-60     20      
AMD-60    51      
AMD-60    50      
AMD-62    0           
1
  • Can you please show what you have tried so far ? Commented Jul 22, 2021 at 13:06

1 Answer 1

1

Try:

df = pd.DataFrame({'Number' : ['IN-60','IN-60','AMD-60','AMD-60','IN-60','AMD-60','AMD-62'],
                   'Value': ['0','10','51','50','20','0','0']
                   })

df = df[~((df["Number"].duplicated(keep=False))&(df["Value"]==0))]

>>>df
   Number  Value
1   IN-60     10
2  AMD-60     51
3  AMD-60     50
4   IN-60     20
6  AMD-62      0

Edit:

If your numbers are strings as in your example, try:

df = df[~((df["Number"].duplicated(keep=False))&(df["Value"]=='0'))]

Edit 2:

For rows that have 2 or more 0 values, you can append back one of them like so:

keep_rows = df.where(df[df["Value"]=="0"].duplicated()).dropna()
df = pd.concat([df[~((df["Number"].duplicated(keep=False))&(df["Value"]=='0'))], keep_rows])
>>> df
   Number Value
1   IN-60    10
2  AMD-60    51
3  AMD-60    50
4   IN-60    20
7  AMD-62     0
Sign up to request clarification or add additional context in comments.

5 Comments

df are same after executing df[~((df["Number"].duplicated(keep=False))&(df["Value"]==0))].
You have to assign it back to df. See my edited answer.
I did it but still not working check it again.
@AkshayTarpara - I think your numbers are strings. See my edit and try that.
Thank you.your answer is right but i have some issue in this. If 2 entry of invoice number are same with zero and try to this solution so both same row are drop but i want to 1 entry this number. I edited my question please see this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.