Instead of delete, we create a new dataframe with only User Name, City and Gender in it, effectively “delete” the other two columns. Let’s say we want to delete Country and Age columns. However, the ending result is the same as a deletion.Ĭonsider our original dataframe, which has 5 columns, namely: This is not a true delete method, but rather a re-assignment operation. del dfĥ Mary Jane Toronto F 30 Delete columns from pandas dataframe with Re-assignment methodĪka the Square bracket method I coined. Note that when using del, the object is deleted so it means the original dataframe is also updated to reflect the delete. We can use it to delete a column from a dataframe. The del a keyword in Python, which can be used to delete an object. If you want to overwrite the original dataframe, include inplace=True argument df.drop('Country', axis=1) # delete a single columnĭf.drop(, axis=1) # delete multiple columnsĭf.drop(, axis=1, inplace=True) # overwrite the original dataframe Delete columns from pandas dataframe with del keyword.To delete multiple columns: pass in a list of the names for the columns to be deleted.To delete a single column: pass in the column name (string).The only difference is that in the method we need to specify an argument axis=1. Similar to deleting rows, we can also delete columns using. Mary Jane CANADA Toronto F 30 Delete columns from dataframe with. import pandas as pdĭf = pd.read_excel('users.xlsx', index_col=0) Feel free to download this sample Excel file to follow along. We’ll start off by creating a dataframe to demonstrate how to delete columns.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |