- Drop column in pandas python – Drop single & multiple columns
- Create Dataframe
- Drop Single Column:
- Delete a column based on column name:
- Drop multiple columns based on column name in pandas
- Drop multiple columns based on column index in pandas
- Drop multiple columns between two column names in pandas
- Drop multiple columns that starts with character in pandas
- Drop multiple columns that ends with character in pandas
- Drop multiple columns that contains a character (like%) in pandas
- Drop columns using regular expression in pandas – regex
- Author
- Related Posts:
- pandas.DataFrame.drop#
- Как удалить столбцы в Pandas (4 примера)
- Пример 1. Удаление одного столбца по имени
- Пример 2. Удаление нескольких столбцов по имени
- Пример 3. Удаление одного столбца по индексу
- Пример 4. Удаление нескольких столбцов по индексу
Drop column in pandas python – Drop single & multiple columns
Delete or drop column in python pandas by done by using drop() function. Here we will focus on Drop single and multiple columns in pandas using index (iloc() function), column name(ix() function) and by position. Drop column name that starts with, ends with, contains a character and also with regular expression and like% function. Let’s see example of each.
- Drop or delete column in pandas by column name using drop() function.
- Drop single and multiple columns in pandas by column index .
- Drop or delete multiple columns between two column index using iloc() function.
- Drop multiple columns between two column names using loc() and ix() function.
- Drop column name which starts with, ends with and contains a character.
- Drop by column name using regular expression.
First let’s create dataframe
Create Dataframe
import pandas as pd import numpy as np #Create a DataFrame d = < 'Name':['Alisa','Bobby','jodha','jack','raghu','Cathrine', 'Alisa','Bobby','kumar','Alisa','Alex','Cathrine'], 'Country' : ["USA","UK","Germany","USA","India","France","USA","UK","India","USA","Canada","France"], 'Age':[26,24,23,22,23,24,26,24,22,23,24,24], 'Score':[85,63,55,74,31,77,85,63,42,62,89,77], 'Scholarship':['Yes','No','Yes','Yes','Yes','No','No','Yes','No','No','Yes','Yes']>df = pd.DataFrame(d,columns=['Name','Country','Age','Score','Scholarship']) df
The resultant dataframe will be
Drop Single Column:
Delete or drop column in pandas by column name using drop() function
Let’s see an example of how to drop a column by name in python pandas
# drop a column based on name df.drop('Age',axis=1)
The above code drops the column named ‘Age’, the argument axis=1 denotes column, so the resultant dataframe will be
Drop single column in pandas by using column index
Let’s see an example on dropping the column by its index in python pandas
# drop a column based on column index df.drop(df.columns[3],axis=1)
In the above example column with index 3 is dropped(4 th column). So the resultant dataframe will be
Delete a column based on column name:
# delete a column del df['Age'] df
In the above example column with the name ‘Age’ is deleted. So the resultant dataframe will be
Drop multiple columns based on column name in pandas
Let’s see an example of how to drop multiple columns by name in python pandas
''' drop multiple column based on name''' df.drop(['Age', 'Score'], axis = 1)
The above code drops the columns named ‘Age’ and ’Score’. The argument axis=1 denotes column, so the resultant dataframe will be
Drop multiple columns based on column index in pandas
Let’s see an example of how to drop multiple columns by index.
''' drop multiple columns based on column index''' df.drop(df.columns[[1,3]], axis = 1)
In the above example column with index 1 (2 nd column) and Index 3 (4 th column) is dropped. So the resultant dataframe will be
Drop multiple columns with index in pandas
Let’s see an example of how to drop multiple columns between two index using iloc() function
''' Remove columns between two column using index - using iloc() ''' df.drop(df.iloc[:, 1:3], axis = 1)
In the above example column with index 1 (2 nd column) and Index 2 (3 rd column) is dropped. So the resultant dataframe will be
Drop multiple columns between two column names in pandas
Let’s see an example of how to drop multiple columns between two column name using ix() function and loc() function
''' Remove columns between two column using column name - using ix() ''' df.drop(df.ix[:, 'Country':'Score'].columns, axis = 1)
''' Remove columns between two column using column name - using loc() ''' df.drop(df.loc[:, 'Country':'Score'].columns, axis = 1)
In the above example column name starting from “country” ending till “score” is removed. So the resultant dataframe with 3 columns removed will be
Drop multiple columns that starts with character in pandas
Let’s see an example of how to drop multiple columns that starts with a character in pandas using loc() function
''' drop column name starts with a character ''' df.loc[:,~ df.columns.str.startswith('A')]
In the above example column name starting with “A” will be dropped. So the resultant dataframe will be
Drop multiple columns that ends with character in pandas
Let’s see an example of how to drop multiple columns that ends with a character using loc() function
''' drop column name ends with a character''' df.loc[:,~df.columns.str.endswith('e')]
In the above example column name ending with “e” will be dropped. So the resultant dataframe will be
Drop multiple columns that contains a character (like%) in pandas
Let’s see an example of how to drop multiple columns that contains a character (like%) in pandas using loc() function
''' drop column name contains ---- drop column name like% in''' df.loc[:,~df.columns.str.contains('sc',case =False)]
In the above example column name that contains “sc” will be dropped. case=False indicates column dropped irrespective of case. So the resultant dataframe will be
Drop columns using regular expression in pandas – regex
Let’s see an example of how to drop columns using regular expressions – regex.
''' drop column name using regular expression ''' df[df.columns.drop(list(df.filter(regex="(Sc)+?.+")))]
In the above example column starts with “sc” will be dropped using regular expressions. So the resultant dataframe will be
Author
With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark. View all posts
Related Posts:
pandas.DataFrame.drop#
Remove rows or columns by specifying label names and corresponding axis, or by directly specifying index or column names. When using a multi-index, labels on different levels can be removed by specifying the level. See the user guide for more information about the now unused levels.
Parameters : labels single label or list-like
Index or column labels to drop. A tuple will be used as a single label and not treated as a list-like.
Whether to drop labels from the index (0 or ‘index’) or columns (1 or ‘columns’).
index single label or list-like
Alternative to specifying axis ( labels, axis=0 is equivalent to index=labels ).
columns single label or list-like
Alternative to specifying axis ( labels, axis=1 is equivalent to columns=labels ).
level int or level name, optional
For MultiIndex, level from which the labels will be removed.
inplace bool, default False
If False, return a copy. Otherwise, do operation in place and return None.
errors , default ‘raise’
If ‘ignore’, suppress error and only existing labels are dropped.
Returns : DataFrame or None
DataFrame without the removed index or column labels or None if inplace=True .
If any of the labels is not found in the selected axis.
Label-location based indexer for selection by label.
Return DataFrame with labels on given axis omitted where (all or any) data are missing.
Return DataFrame with duplicate rows removed, optionally only considering certain columns.
Return Series with specified index labels removed.
>>> df = pd.DataFrame(np.arange(12).reshape(3, 4), . columns=['A', 'B', 'C', 'D']) >>> df A B C D 0 0 1 2 3 1 4 5 6 7 2 8 9 10 11
>>> df.drop(['B', 'C'], axis=1) A D 0 0 3 1 4 7 2 8 11
>>> df.drop(columns=['B', 'C']) A D 0 0 3 1 4 7 2 8 11
>>> df.drop([0, 1]) A B C D 2 8 9 10 11
Drop columns and/or rows of MultiIndex DataFrame
>>> midx = pd.MultiIndex(levels=[['llama', 'cow', 'falcon'], . ['speed', 'weight', 'length']], . codes=[[0, 0, 0, 1, 1, 1, 2, 2, 2], . [0, 1, 2, 0, 1, 2, 0, 1, 2]]) >>> df = pd.DataFrame(index=midx, columns=['big', 'small'], . data=[[45, 30], [200, 100], [1.5, 1], [30, 20], . [250, 150], [1.5, 0.8], [320, 250], . [1, 0.8], [0.3, 0.2]]) >>> df big small llama speed 45.0 30.0 weight 200.0 100.0 length 1.5 1.0 cow speed 30.0 20.0 weight 250.0 150.0 length 1.5 0.8 falcon speed 320.0 250.0 weight 1.0 0.8 length 0.3 0.2
Drop a specific index combination from the MultiIndex DataFrame, i.e., drop the combination ‘falcon’ and ‘weight’ , which deletes only the corresponding row
>>> df.drop(index=('falcon', 'weight')) big small llama speed 45.0 30.0 weight 200.0 100.0 length 1.5 1.0 cow speed 30.0 20.0 weight 250.0 150.0 length 1.5 0.8 falcon speed 320.0 250.0 length 0.3 0.2
>>> df.drop(index='cow', columns='small') big llama speed 45.0 weight 200.0 length 1.5 falcon speed 320.0 weight 1.0 length 0.3
>>> df.drop(index='length', level=1) big small llama speed 45.0 30.0 weight 200.0 100.0 cow speed 30.0 20.0 weight 250.0 150.0 falcon speed 320.0 250.0 weight 1.0 0.8
Как удалить столбцы в Pandas (4 примера)
Вы можете использовать функцию drop() , чтобы удалить один или несколько столбцов из кадра данных pandas:
#drop one column by name df.drop('column_name', axis= 1 , inplace= True ) #drop multiple columns by name df.drop(['column_name1', 'column_name2'], axis= 1 , inplace= True ) #drop one column by index df.drop (df.columns [[0]], axis= 1 , inplace= True ) #drop multiple columns by index df.drop (df.columns[[0,2,5]], axis= 1 , inplace= True )
Обратите внимание на следующее:
- Аргумент оси указывает, следует ли удалить строки (0) или столбцы (1).
- Аргумент inplace указывает, что столбцы должны быть удалены без переназначения DataFrame.
В следующих примерах показано, как использовать эту функцию на практике со следующими пандами DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame() #view DataFrame df A B C 0 25 5 11 1 12 7 8 2 15 7 10 3 14 9 6 4 19 12 6 5 23 9 5 6 25 9 9 7 29 4 12
Пример 1. Удаление одного столбца по имени
В следующем коде показано, как удалить один столбец из DataFrame по имени:
#drop column named 'B' from DataFrame df.drop('B', axis= 1 , inplace= True ) #view DataFrame df A C 0 25 11 1 12 8 2 15 10 3 14 6 4 19 6 5 23 5 6 25 9 7 29 12
Пример 2. Удаление нескольких столбцов по имени
В следующем коде показано, как удалить несколько столбцов по имени:
#drop columns 'A' and 'C' from DataFrame df.drop(['A', 'C'], axis= 1 , inplace= True ) #view DataFrame df B 0 5 1 7 2 7 3 9 4 12 5 9 6 9 7 4
Пример 3. Удаление одного столбца по индексу
В следующем коде показано, как удалить один столбец по индексу:
#drop first column from DataFrame df.drop (df.columns [[0]], axis= 1 , inplace= True ) #view DataFrame df B C 0 5 11 1 7 8 2 7 10 3 9 6 4 12 6 5 9 5 6 9 9 7 4 12
Пример 4. Удаление нескольких столбцов по индексу
В следующем коде показано, как удалить несколько столбцов по индексу:
#drop multiple columns from DataFrame df.drop (df.columns [[0, 1]], axis= 1 , inplace= True ) #view DataFrame df C 0 11 1 8 2 10 3 6 4 6 5 5 6 9 7 12