0

EDIT : Sorry, I got confused rows with Columns,(noob here)

I have a CSV , where there are 3 columns, heading, image and description .

Some of the items in description is Empty , and I want to delete the complete column if description is empty or has length < 1

Firstly, I got all columns which has length of description < 1 using this Code :

for i in df['description']:
if len(i) < 1 :
    print('Empty')

Output :

Empty
Empty

This means there are 2 columns which has description of length < 1 , Now i try to remove them :

for i in df['description']:
if len(i) < 1 :
    df.drop(i, inplace=True, axis=1)

But, still there are those 2 columns with Empty data, how to remove them, What is wrong in my code ? Please Guide

also tried :

df = df.drop(df.columns[[i]], axis=1)

but, nothing works

1 Answer 1

2

I think there is a little of confusion. You say you want to remove the entire column, but this means deleting the description column; instead, I think you're talking about the row with empty description.

If this is your case, then you just need to cycle over your dataframe (with an index too) and check if the description is null or empty; if that's true, you can drop the row in-place (index=N means i, axis=0).

for i, description in zip(df.index, df['description']):
    if not description:  # True if None and empty string
        df.drop(index=i, inplace=True)

This is an example of what you're getting.

before drop:
  heading image description
0     foo     a        None
1     bar     b            
2     baz     c   some text

after drop:
  heading image description
2     baz     c   some text

If, however, you need to remove the entire column if there is a None/empty string, then you need to check first if there are values that match your search, and if yes, remove that column.

df.applymap() applies a function to all DataFrame, then you keep only the description column, and you got a bool Series. With any() you check if at least one element is True (None or empty string). If yes, drop all the description column.

if any(df.applymap(lambda x: not x)['description']):
    df.drop(columns='description', inplace=True)

Again, here's an example of result.

before drop:
  heading image description
0     foo     a        None
1     bar     b            
2     baz     c   some text

after drop:
  heading image
0     foo     a
1     bar     b
2     baz     c
Sign up to request clarification or add additional context in comments.

5 Comments

Sorry, I got confused rows with Columns,(noob here) . I wanted to delete the row and not column
Following Your Code , I tried this : for i in df['description']: if len(i) < 1 : df.drop(index=i, inplace=True) , still it has the items. not deleted
I gave you the right answer with an example of result. By the way, you're not cycling over index (i), but over values (None, '', 'some text', etc)
How to write correct One Sir, please Guide
Just check the first code block in my answer, that's the correct for loop

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.