Delete a Column from a Pandas Dataframe

Delete a column from a Pandas DataFrame

As you've guessed, the right syntax is

del df['column_name']

It's difficult to make del df.column_name work simply as the result of syntactic limitations in Python. del df[name] gets translated to df.__delitem__(name) under the covers by Python.

What is the best way to remove columns in pandas

Follow the doc:

DataFrame is a 2-dimensional labeled data structure with columns of potentially different types.

And pandas.DataFrame.drop:

Drop specified labels from rows or columns.

So, I think we should stick with df.drop. Why? I think the pros are:

  1. It gives us more control of the remove action:

    # This will return a NEW DataFrame object, leave the original `df` untouched.
    df.drop('a', axis=1)
    # This will modify the `df` inplace. **And return a `None`**.
    df.drop('a', axis=1, inplace=True)
  2. It can handle more complicated cases with it's args. E.g. with level, we can handle MultiIndex deletion. And with errors, we can prevent some bugs.

  3. It's a more unified and object oriented way.


And just like @jezrael noted in his answer:

Option 1: Using key word del is a limited way.

Option 3: And df=df[['b','c']] isn't even a deletion in essence. It first select data by indexing with [] syntax, then unbind the name df with the original DataFrame and bind it with the new one (i.e. df[['b','c']]).

How to delete a column from a data frame with pandas?

To actually delete the column

del df['id'] or df.drop('id', 1) should have worked if the passed column matches exactly

However, if you don't need to delete the column then you can just select the column of interest like so:

In [54]:

df['text']
Out[54]:
0 text1
1 text2
2 textn
Name: text, dtype: object

If you never wanted it in the first place then you pass a list of cols to read_csv as a param usecols:

In [53]:
import io
temp="""id text
363.327 text1
366.356 text2
37782 textn"""
df = pd.read_csv(io.StringIO(temp), delimiter='\s+', usecols=['text'])
df
Out[53]:
text
0 text1
1 text2
2 textn

Regarding your error it's because 'id' is not in your columns or that it's spelt differently or has whitespace. To check this look at the output from print(df.columns.tolist()) this will output a list of the columns and will show if you have any leading/trailing whitespace.

Removing a column permanently from a data frame in Python

You have to assign it back to mydf, if you want to reach a permanent change, i.e. do

mydf = mydf.drop('Z', axis=1)

instead.

How to delete a certain value in a cell in columns of csv using pandas

You could achieve it this way,

f["language"] = f.apply(
lambda x: ",".join(filter(lambda y: y != "None", x.language.split(","))), axis=1
)

Or much better

f["language"] = f.apply(
lambda x: ",".join([y for y in x.language.split(",") if y != "None"]), axis=1
)

every value is a Object... want to delete first column pandas dataframe

If you want to display the values of a specific column of a DataFrame :

df['Start Time'].values.tolist()

To refresh the index, you can use :

df = df.reset_index(drop=True)

And to change the type of a specific column (to int/float/str ...) :

df['column_name'] = df['column_name'].astype(int)


Related Topics



Leave a reply



Submit