0

I need to drop rows in a dataframe created using panda in Python. I've tried all the different ways mentioned in different forums but nothing seems to work.

I want to delete the rows with ZipCodes more than 5 digits long, so I saw this as an answer:

df = df[len(df.CoulmnName) <5]
df.head

and I get Error: "KeyError: False" And I want to iterate through the dataframe to delete rows where there are values greater than 1 in different columns. This is what I tried:

for x in cleandata4bestdeal.columns:
if x[line] > 1:
   df.drop(df[line])

and it doesn't work. I want to iterate through the column headers to remove the rows with values that are not 1's or 0's.

2 Answers 2

1

len(df.ZipCode) is going to give you the column length, not the length of each row. If you want the latter, you'll need to perform the str.len operation on it -

mask = df.ZipCode.astype(str).str.len() <= 5  # retain zipCodes with 5 or less digits

Now, with the mask, you can filter out rows you don't wish to keep:

df = df.loc[mask]  # df = df[mask]
Sign up to request clarification or add additional context in comments.

8 Comments

I did what you wrote but it only gave me the column header as an output, Idk why?
@israabuhasna Maybe because you don't have any zips <= 5? Can you post some of your data?
Nevermind it worked, so I can use this to iterate over the column headers with the condition that non of the values go above 1?
@israabuhasna Iteration? Nah you don't need to iterate. Just df = df[mask] will remove all rows you don't need.
@Coldspeed Yeah but now I only want 1's or 0's in the columns after ZipCode. So i want to iterate through the column names to delete any wrong data rows: clmlst = df.columns.tolist() for x in clmlst[2,:]: df = df[(df[x] <= 1)] I'm still getting errors
|
1

By using .str.len()

df[df.zip.astype(str).str.len()<5]
Out[336]: 
    zip
0  1111
2  1111
3  2222
4  3333

Data input

df=pd.DataFrame({'zip':[1111,111111,1111,2222,3333]})

Comments

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.