Course Content
Pandas First Steps
Pandas First Steps
Deleting a Row/Column
At times, certain columns may not provide valuable information, making it advantageous to remove them. The pandas
library offers the drop()
method for this purpose.
index
: specifies the row indexes to be deleted (used whenaxis=0
);columns
: identifies the column names to be deleted (used whenaxis=1
);axis
: choose whether to remove labels from the rows (0
) or columns (1
). The default is0
.
We'll start by examining the DataFrame:
import pandas as pd countries_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : [None, None, 'Europe', None, 'Europe', None, 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(countries_data) print(countries)
We notice that the 'continent'
column contains numerous missing values, making it less informative. Consequently, we'll remove it.
import pandas countries_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : [None, None, 'Europe', None, 'Europe', None, 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pandas.DataFrame(countries_data) countries = countries.drop(columns = ['continent'],axis=1) print(countries)
Swipe to show code editor
We have a DataFrame called audi_cars
. Your task is to remove the 'capital'
column.
Solution
Thanks for your feedback!
Deleting a Row/Column
At times, certain columns may not provide valuable information, making it advantageous to remove them. The pandas
library offers the drop()
method for this purpose.
index
: specifies the row indexes to be deleted (used whenaxis=0
);columns
: identifies the column names to be deleted (used whenaxis=1
);axis
: choose whether to remove labels from the rows (0
) or columns (1
). The default is0
.
We'll start by examining the DataFrame:
import pandas as pd countries_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : [None, None, 'Europe', None, 'Europe', None, 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(countries_data) print(countries)
We notice that the 'continent'
column contains numerous missing values, making it less informative. Consequently, we'll remove it.
import pandas countries_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : [None, None, 'Europe', None, 'Europe', None, 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pandas.DataFrame(countries_data) countries = countries.drop(columns = ['continent'],axis=1) print(countries)
Swipe to show code editor
We have a DataFrame called audi_cars
. Your task is to remove the 'capital'
column.
Solution
Thanks for your feedback!