How to drop ROW and COLUMN in a Pandas DataFrame in Python
To drop a row or column in a Pandas DataFrame in Python, you can use the drop
method. The drop
method takes two main arguments: the index or labels of the rows/columns to be removed, and the axis (0 for rows and 1 for columns) on which the operation should be performed.
For example, if you want to drop the third row in a DataFrame df
, you would use the following code:
df.drop(2, axis=0, inplace=True)
This will remove the third row from the DataFrame, and the inplace=True
argument is used to make the change permanent.
Similarly, If you want to drop a column from a DataFrame, you can pass the column label in the drop()
method with axis=1
df.drop("column_name", axis=1, inplace=True)
Note that the drop()
method does not modify the original DataFrame by default, so if you want the changes to be permanent, you should set the inplace
parameter to True
.
Also to drop multiple rows and columns, you can pass the list of index/label as input in the drop method, this will drop all the rows/columns in the list.
df.drop([2,3], axis=0, inplace=True)
df.drop(["column1","column2"], axis=1, inplace=True)
It’s important to be careful when removing rows or columns from a DataFrame, as it can have a significant impact on the results of any further analysis that is performed on the data.
In this Learn through Codes example, you will learn: How to drop ROW and COLUMN in a Pandas DataFrame in Python.
Essential Gigs
For only $50, Nilimesh will develop time series forecasting model for you using python or r. | Note: please contact me…www.fiverr.com
For only $50, Nilimesh will do your data analytics and econometrics projects in python. | Note: please contact me…www.fiverr.com
For only $50, Nilimesh will do your machine learning and data science projects in python. | Note: please contact me…www.fiverr.com
For only $50, Nilimesh will do your gis and spatial programming projects in python. | Note: please contact me before…www.fiverr.com