We learned how to add columns, but what if wanted to remove them?

Question

In the lesson, we learned how to add columns to a dataframe, but what if wanted to remove them?

Answer

In addition to being able to add columns, we have the ability to remove columns of a dataframe in Pandas.

There are a few ways you can go about removing columns from a dataframe:

  • Creating a new dataframe, and including just the columns you want to keep from the original dataframe. For example, if we only wanted to include these columns from a dataframe, it effectively “removes” all the other columns not included:
    new_df = df[['col1', 'col4']]

  • You can utilize the built-in drop() method, to delete a specific column. In order to drop a column, we must specify axis=1. We can do so as follows:
    df.drop('col3', axis=1, inplace=True)

  • To drop multiple columns at once, we can enter in multiple column names as a list using drop(), like so:
    df.drop(['col3', 'col5'], axis=1, inplace=True)

5 Likes

I used the following code…can you please tell me what is wrong with that @jephos249

salutation = lambda row: 'Dear Mr. ’ + row[‘last_name’] if row == ‘male’ else 'Dear Ms. ’ + row[‘last_name’]

orders[‘salutation’] = orders.gender.apply(salutation)

Error it’s showing

string indices must be integers, not str

apply must indicate to which to apply: adding " , axis = 1 " after salutation,

Why does f-string formatting not work?

orders['salutation'] = orders.apply(lambda row: 'Dear Mr. {}'.format(row.last_name) if row.gender == 'male' else 'Dear Ms. {}'.format(row.last_name), axis=1)

Works fine. But with f-string formatting is says it’s wrong:

orders['salutation'] = orders.apply(lambda row: f'Dear Mr. {row.last_name}' if row.gender == 'male' else f'Dear Ms. {row.last_name}', axis=1)

1 Like

I just got here and I tried the same thing. I would like to know the reasoning behind why this doesn’t work.

1 Like

orders[‘salutation’]= orders.apply(lambda x:
f"Dear Mr. {x[‘last_name’]}" if x.gender ==‘male’ else f"Dear Ms. {x[‘last_name’]}",axis=1)

above code works fine for me.

The difference is accessing last_name values
x[‘last_name’] —> works fine
x.last_name —> doesn’t work

You missed row.gender == “male” in your lambda function and axis = 1 in your apply method.

Your code was applied to the gender series/column in the orders data frame whereas it has to be applied to the data frame. It should be:

salutation = lambda row: ‘Dear Mr.’ + row[‘last_name’] if row[‘gender’] == ‘male’ else ‘Dear Ms.’ + row[‘last_name’]

orders[‘salutation’] = orders.apply(salutation, axis=1)

Happy learning!