Dropping columns
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.
In this exercise, you'll 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 for you.
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Analyzing Police Activity with pandas
คำแนะนำการฝึกหัด
- Examine the DataFrame's
.shapeto find out the number of rows and columns. - Drop both the
county_nameandstatecolumns by passing the column names to the.drop()method as a list of strings. - Examine the
.shapeagain to verify that there are now two fewer columns.
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Examine the shape of the DataFrame
print(ri.____)
# Drop the 'county_name' and 'state' columns
ri.____([____, ____], axis='columns', inplace=True)
# Examine the shape of the DataFrame (again)
print(____)