Home > Blockchain >  Unable to drop rows with missing (0) value -Dataframe
Unable to drop rows with missing (0) value -Dataframe

Time:05-01

I tried to drop the row with missing values (0) in them but it didn't work. When I extracted to an excel file, it is still showing a lot of rows with 0 value in my variable GTCBSA. Did I write my drop function incorrectly?

for i in range(2004,2007):
   url = f"https://api.census.gov/data/{i}/cps/basic/jun?get=GTCBSA,PEFNTVTY"
   a = pd.read_json(url)
   a = pd.DataFrame(a.iloc[1:,]).set_axis(a.iloc[0,], axis="columns", inplace=False)
   a.dropna(subset = ["GTCBSA"])

a
   

enter image description here

CodePudding user response:

What you are using would only give you results if the values are actually missing like being (Nan). Not if the value is 0.

Based on the answers above checking that the value is equal to zero also is not working for you. The only thing I can think of is that the values are maybe strings.

I think you should try something like this:

df = df[~(df['GTCBSA'] == '0')]

CodePudding user response:

You can use

df = df[df['GTCBSA'].ne(0)]

# or

df = df[~df['GTCBSA'].eq(0)]

Since your column type is object, as I said in the comment, you can use

df = df[df['GTCBSA'].ne('0')]

# or

df = df[df['GTCBSA'].astype(int).ne(0)]

Also I have advised you to check the column type in my second comments...

  • Related