Python drop column by index

How to Drop Multiple Columns by Index in pandas

In this pandas drop multiple columns by index article, I will explain how to drop multiple columns by index with several DataFrame examples. You can drop columns by index by using DataFrame.drop() method and by using DataFrame.iloc[].columns property to get the column names by index.

1. Quick Examples of Pands Drop Multiple Columns by Index

If you are in hurry, below are some quick examples of how to drop multiple columns by an index of DataFrame.

 # Below are some quick examples. # Drop columns based on column index. df2 = df.drop(df.columns[[0, 1, 2]],axis = 1) # Drop column of index using DataFrame.iloc[] and drop() methods. df2 = df.drop(df.iloc[:, 1:3],axis = 1) 

Now, let’s create a Pandas DataFrame with a few rows and columns and execute some examples and validate results. Our DataFrame contains column names Courses , Fee and Discount .

 # Create a Pandas DataFrame. import pandas as pd import numpy as np technologies= < 'Courses':["Spark","Spark","PySpark","JAVA","Hadoop",".Net","Python","AEM","Oracle","SQL DBA","C","WebTechnologies"], 'Fee' :[22000,25000,23000,24000,26000,30000,27000,28000,35000,32000,20000,15000], 'Duration':['30days','35days','40days','45days','50days','55days','60days','35days','30days','40days','50days','55days'] >df = pd.DataFrame(technologies) print(df) 
 # Output: Courses Fee Duration 0 Spark 22000 30days 1 Spark 25000 35days 2 PySpark 23000 40days 3 JAVA 24000 45days 4 Hadoop 26000 50days 5 .Net 30000 55days 6 Python 27000 60days 7 AEM 28000 35days 8 Oracle 35000 30days 9 SQL DBA 32000 40days 10 C 20000 50days 11 WebTechnologies 15000 55days 

2. Pandas Drop Multiple Columns By Index

In this section, you’ll learn how to drop multiple columns by index in pandas. You can use df.columns[[index1, index2, indexn]] to identify the list of column names in that index position and pass that list to the drop method.

Note that an index is 0 based. Use 0 to delete the first column and 1 to delete the second column and so on.

 # Drop columns based on column index. df2 = df.drop(df.columns[[1, 2]],axis = 1) print(df2) 
 # Output: Courses 0 Spark 1 Spark 2 PySpark 3 JAVA 4 Hadoop 5 .Net 6 Python 7 AEM 8 Oracle 9 SQL DBA 10 C 11 WebTechnologies 

3. Drop Columns by Index Using iloc[] and drop() Methods

If you wanted to drop columns from starting and ending index ranges, you can do so by using DataFrame.iloc[] property. This property returns all column names between specified indexes and pass these column names to drop() method.

 # Drop column of index using DataFrame.iloc[] and drop() methods. df2 = df.drop(df.iloc[:, 1:3],axis = 1) print(df2) 
 # Output: Courses 0 Spark 1 Spark 2 PySpark 3 JAVA 4 Hadoop 5 .Net 6 Python 7 AEM 8 Oracle 9 SQL DBA 10 C 11 WebTechnologies 

4. Drop Columns by Index Using DataFrame.loc[] and drop() Methods

Similarly, you can drop columns by the range of labels using DataFrame.loc[] and DataFrame.drop() methods. Here the loc[] property is used to access a group of rows and columns by label(s) or a boolean array.

 # Drop columns of index using DataFrame.loc[] and drop() methods. df2 = df.drop(df.loc[:, 'Courses':'Fee'].columns,axis = 1) print(df2) 
 # Output: Duration 0 30days 1 35days 2 40days 3 45days 4 50days 5 55days 6 60days 7 35days 8 30days 9 40days 10 50days 11 55days 

5. Complete Examples Drop Multiple Columns By Index

 # Create a Pandas DataFrame. import pandas as pd import numpy as np technologies= < 'Courses':["Spark","Spark","PySpark","JAVA","Hadoop",".Net","Python","AEM","Oracle","SQL DBA","C","WebTechnologies"], 'Fee' :[22000,25000,23000,24000,26000,30000,27000,28000,35000,32000,20000,15000], 'Duration':['30days','35days','40days','45days','50days','55days','60days','35days','30days','40days','50days','55days'] >df = pd.DataFrame(technologies) print(df) # Drop Multiple Columns by labels. df2 = df.drop(['Courses', 'Duration'],axis = 1) print(df2) # Drop columns based on column index. df2 = df.drop(df.columns[[0, 1, 2]],axis = 1) print(df2) # Drop column by index using DataFrame.iloc[] and drop() methods. df2 = df.drop(df.iloc[:, 1:3],axis = 1) print(df2) # Drop columns by labels using DataFrame.loc[] and drop() methods. df2 = df.drop(df.loc[:, 'Courses':'Fee'].columns,axis = 1) print(df2) 

Conclusion

In this article, You have learned how to drop multiple columns by index using, DataFrame.drop() Method and DataFrame.iloc[] and DataFrame.loc[] properties with some examples.

Читайте также:  Html знак стрелка вниз

References

You may also like reading:

Источник

pandas.DataFrame.drop#

Remove rows or columns by specifying label names and corresponding axis, or by specifying directly 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 inplace 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=[['lama', '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 lama 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 lama 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 lama 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 lama 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 

Источник

Читайте также:  Python selenium update page

How to Drop Column(s) by Index in pandas

In this pandas drop columns by index article, I will explain how to drop columns by index with several DataFrame examples. You can drop column by index in pandas by using DataFrame.drop() method and by using DataFrame.iloc[].columns property to get the column names by index.

  • drop() method is used to remove columns or rows from DataFrame.
  • Use axis param to specify what axis you would like to remove. By default axis = 0 meaning to remove rows. Use axis=1 or columns param to remove columns.
  • Use inplace=True to remove row/column in place meaning on existing DataFrame with out creating copy.

1. Quick Examples of Drop Columns by Index in Pandas DataFrame

If you are in hurry, below are some quick examples to drop column(s) by an index of pandas DataFrame.

 # Below are some quick examples. # Using DataFrame.drop() method. df2=df.drop(df.columns[1], axis=1) # Drop columns based on column index. df2 = df.drop(df.columns[[0, 1, 2]],axis = 1) # Drop column of index using DataFrame.iloc[] and drop() methods. df2 = df.drop(df.iloc[:, 1:3],axis = 1) # Drop columns by labels using DataFrame.loc[] and drop() methods. df2 = df.drop(df.loc[:, 'Courses':'Fee'].columns,axis = 1) 

Now, let’s create a Pandas DataFrame with a few rows and columns and execute some examples and validate results. Our DataFrame contains column names Courses , Fee and Discount .

 # Create a Pandas DataFrame. import pandas as pd import numpy as np technologies= < 'Courses':["Spark","Spark","PySpark","JAVA","Hadoop",".Net","Python","AEM","Oracle","SQL DBA","C","WebTechnologies"], 'Fee' :[22000,25000,23000,24000,26000,30000,27000,28000,35000,32000,20000,15000], 'Duration':['30days','35days','40days','45days','50days','55days','60days','35days','30days','40days','50days','55days'] >df = pd.DataFrame(technologies) print(df) 
 # Output: Courses Fee Duration 0 Spark 22000 30days 1 Spark 25000 35days 2 PySpark 23000 40days 3 JAVA 24000 45days 4 Hadoop 26000 50days 5 .Net 30000 55days 6 Python 27000 60days 7 AEM 28000 35days 8 Oracle 35000 30days 9 SQL DBA 32000 40days 10 C 20000 50days 11 WebTechnologies 15000 55days 

2. Using DataFrame.drop() Column by Index

you can use DataFrame.drop() function to remove the column by index. The drop() function is used to drop specified labels from rows or columns. Remove rows or columns by specifying label names and corresponding axis, or by specifying directly index of columns. When using a multi-index, labels on different levels can be removed by specifying the level.

Читайте также:  Pointers to functions in cpp

Note that an index is 0 based. Use 0 to delete the first column and 1 to delete the second column and so on.

 # Using DataFrame.drop() method. df2=df.drop(df.columns[1], axis=1) print(df2) 

Yields below output. This deletes the second column as the index starts from 0.

 # Output: Courses Duration 0 Spark 30days 1 Spark 35days 2 PySpark 40days 3 JAVA 45days 4 Hadoop 50days 5 .Net 55days 6 Python 60days 7 AEM 35days 8 Oracle 30days 9 SQL DBA 40days 10 C 50days 11 WebTechnologies 55days 

3. Drop Multiple Columns By Index

In this section, you’ll learn how to drop multiple columns by index. You can use df.columns[[index1, index2, indexn]] to identify the list of column names in that index position and pass that list to the drop method.

 # Drop columns based on column index. df2 = df.drop(df.columns[[1, 2]],axis = 1) print(df2) 
 # Output: Courses 0 Spark 1 Spark 2 PySpark 3 JAVA 4 Hadoop 5 .Net 6 Python 7 AEM 8 Oracle 9 SQL DBA 10 C 11 WebTechnologies 

4. Drop Columns by Index Using DataFrame.iloc[] and drop() Methods

If you wanted to drop columns from starting and ending index ranges, you can do so by using iloc[] property. This property returns all column names between specified indexes and ass these column names to drop() method.

 # Drop column of index using DataFrame.iloc[] and drop() methods. df2 = df.drop(df.iloc[:, 1:3],axis = 1) print(df2) 
 # Output: Courses 0 Spark 1 Spark 2 PySpark 3 JAVA 4 Hadoop 5 .Net 6 Python 7 AEM 8 Oracle 9 SQL DBA 10 C 11 WebTechnologies 

5. Drop Columns of Index Using DataFrame.loc[] and drop() Methods

Similarly, you can drop columns by the range of labels using DataFrame.loc[] and DataFrame.drop( ) methods. Here the loc[] property is used to access a group of rows and columns by label(s) or a boolean array.

 # Drop columns of index using DataFrame.loc[] and drop() methods. df2 = df.drop(df.loc[:, 'Courses':'Fee'].columns,axis = 1) print(df2) 
 # Output: Duration 0 30days 1 35days 2 40days 3 45days 4 50days 5 55days 6 60days 7 35days 8 30days 9 40days 10 50days 11 55days 

6. Complete Examples of Drop Columns By Index

 # Create a Pandas DataFrame. import pandas as pd import numpy as np technologies= < 'Courses':["Spark","Spark","PySpark","JAVA","Hadoop",".Net","Python","AEM","Oracle","SQL DBA","C","WebTechnologies"], 'Fee' :[22000,25000,23000,24000,26000,30000,27000,28000,35000,32000,20000,15000], 'Duration':['30days','35days','40days','45days','50days','55days','60days','35days','30days','40days','50days','55days'] >df = pd.DataFrame(technologies) print(df) # Using DataFrame.drop() method. df2=df.drop(df.columns[1], axis=1) print(df2) # Drop Multiple Columns by labels. df2 = df.drop(['Courses', 'Duration'],axis = 1) print(df2) # Drop columns based on column index. df2 = df.drop(df.columns[[0, 1, 2]],axis = 1) print(df2) # Drop column by index using DataFrame.iloc[] and drop() methods. df2 = df.drop(df.iloc[:, 1:3],axis = 1) print(df2) # Drop columns by labels using DataFrame.loc[] and drop() methods. df2 = df.drop(df.loc[:, 'Courses':'Fee'].columns,axis = 1) print(df2) 

Conclusion

In this article, You have learned how to drop columns by index using DataFrame.drop() Method and DataFrame.iloc[] and DataFrame.loc[] properties with some examples.

References

You may also like reading:

Источник

Оцените статью