0

I have a dataframe having two columns "Service" and "Value". I want to remove all the rows having "0" under value column.

service    value

abc         10
def          0
ghi          0
xyz          5

I want my dataframe looks like

service    value

abc         10
xyz          5

I tried the following

df = df[(df != 0).all(1)]



df = pd.DataFrame(list(result.items()),columns=['service', 'value'])
df = df[(df != 0).all(1)]

For small Dataframe having 6-7 rows it's working fine but in another Dataframe having 125 rows I am getting the following error.

Illegal instruction

PS: I checked all the values under "value" column and these are numbers.

anky
  • 74,114
  • 11
  • 41
  • 70
niraj pandey
  • 102
  • 7

1 Answers1

1

You can use the drop function combined with a condition :

df = pd.DataFrame(
{'service': ['abc', 'def', 'ghi', 'xyz'],
'value': [10,0,0,5]})
df.drop(df[df.value==0].index)

Out :

service value
0   abc 10
3   xyz 5
vlemaistre
  • 3,301
  • 13
  • 30
  • Is there any dependency of dataframe with number of dictionary keys. After little bit debugging I found when my no of keys goes beyond 31 I saw the error. Here is my code: – niraj pandey May 29 '19 at 07:33
  • I'm not sure I understand your question, but not there is no dependancy. Each dictionnary key corresponds to a new column in my df – vlemaistre May 29 '19 at 07:35
  • I tried the following code with dictionary having 31 keys and it's working fine but when I run the same code with dictionary 32 keys I am getting: "Illegal instructions" import pandas as pd pd.set_option('display.max_rows', None) df = pd.DataFrame(list(result.items()),columns=['Service', '$Amount']) df = df[(df != 0).all(1)] – niraj pandey May 29 '19 at 07:38
  • result = {'a': 1 ,'b': 2, 'c': 3, 'd': 4, 'e': 0, 'f': 2, 'g':0,'h':1,'i': 10,'k':11,'l': 19, 'm': 21,'n': 23,'o':43,'p':35,'q': 43,'r': 4, 's': 2, 't': 0, 'u':67,'v':21,'w':14,'x':0,'y':0,'z':12,'j':134,'as':12,'ad':31,'aw':14,'ds':30, 'asq': 23} For testing you can use this dictionary and to reproduce the issue add one more key value on this dictionary – niraj pandey May 29 '19 at 07:48
  • Ok found the issue but not sure about root cause for this. Seems this is wroking fine on RHEL6 platform while issue with RHEL5 platform. Whenever I am using the same python version on both the platform and using python from a common location (no using local installation) . – niraj pandey May 29 '19 at 08:10