Often a DataFrame will contain columns that are not useful to your analysis. Such columns should be dropped from the DataFrame to make it easier for you to focus on the remaining columns.
The columns can be removed by specifying label names and corresponding axis, or by specifying index or column names directly. When using a multi-index, labels on different levels can be removed by specifying the level.
.drop() Method
Let's compare missing value counts with the shape of the dataframe. You will notice that the county_name column contains as many missing values as rows, meaning that it only contains missing values.
Since it contains no useful information, this column can be dropped using the .drop() method.
Besides specifying the column name, you need to specify that you are dropping from the columns axis and that you want the operation to occur in place, which avoids an assignment statement as shown below:
The .dropna() method is a great way to drop rows based on the presence of missing values in that row.
For example, using the dataset above, let's assume the stop_date and stop_time columns are critical to our analysis, and thus a row is useless to us without that data.
state stop_date stop_time driver_gender driver_race
0RI2005-01-0412:55M White
1RI2005-01-2323:15M White
2RI2005-02-1704:15M White
3RI2005-02-2017:15M White
4RI2005-02-2401:20F White
We can tell pandas to drop all rows that have a missing value in either the stop_date or stop_time column. Because we specify a subset, the .dropna() method only takes these two columns into account when deciding which rows to drop.
In this example, you will drop the county_name column because it only contains missing values, and you'll drop the state column because all of the traffic stops took place in one state (Rhode Island). Thus, these columns can be dropped because they contain no useful information. The number of missing values in each column has been printed to the console for you.
Examine the DataFrame's .shape to find out the number of rows and columns.
Drop both the county_name and state columns by passing the column names to the .drop() method as a list of strings.
Examine the .shape again to verify that there are now two fewer columns.
# Examine the shape of the DataFrameprint(ri.shape)# Drop the 'county_name' and 'state' columns
ri.drop(['county_name','state'], axis='columns', inplace=True)# Examine the shape of the DataFrame (again)print(ri.shape)
Explore data analysis with Python. Pandas DataFrames make manipulating your data easy, from selecting or replacing columns and indices to reshaping your data.
In PySpark, we can drop one or more columns from a DataFrame using the .drop("column_name") method for a single column or .drop(["column1", "column2", ...]) for multiple columns.
Learn how to rename columns in pandas with .rename(), df.columns, and .set_axis(), plus Python functions for snake_case and fixes for KeyError and ValueError.
Often a DataFrame will contain columns that are not useful to your analysis. Such columns should be dropped from the DataFrame to make it easier for you to focus on the remaining columns.
The columns can be removed by specifying label names and corresponding axis, or by specifying index or column names directly. When using a multi-index, labels on different levels can be removed by specifying the level.
.drop()MethodLet's compare missing value counts with the shape of the dataframe. You will notice that the
county_namecolumn contains as many missing values as rows, meaning that it only contains missing values.Since it contains no useful information, this column can be dropped using the
.drop()method.Besides specifying the column name, you need to specify that you are dropping from the columns axis and that you want the operation to occur in place, which avoids an assignment statement as shown below:
.dropna()MethodThe
.dropna()method is a great way to drop rows based on the presence of missing values in that row.For example, using the dataset above, let's assume the stop_date and stop_time columns are critical to our analysis, and thus a row is useless to us without that data.
We can tell pandas to drop all rows that have a missing value in either the
stop_dateorstop_timecolumn. Because we specify a subset, the.dropna()method only takes these two columns into account when deciding which rows to drop.Interactive Example of Dropping Columns
In this example, you will drop the
county_namecolumn because it only contains missing values, and you'll drop thestatecolumn because all of the traffic stops took place in one state (Rhode Island). Thus, these columns can be dropped because they contain no useful information. The number of missing values in each column has been printed to the console for you..shapeto find out the number of rows and columns.county_nameandstatecolumns by passing the column names to the.drop()method as a list of strings..shapeagain to verify that there are now two fewer columns.When you run the above code, it produces the following result:
Try it for yourself.
To learn more about dropping columns in pandas, please see this video from our course, Introduction to Data Visualization with ggplot2.
This content is taken from DataCamp’s Introduction to Data Visualization with ggplot2 course by Kevin Markham.
Check out our Pandas Add Column Tutorial.