Inserting a New Column
We'll now explore another technique for adding a column to a DataFrame. This approach utilizes the insert()
method. A key benefit of the insert()
method is that it allows you to specify the position of the new column within the DataFrame.
df.insert(column_index, 'column_name', [value_1, value_2, value_3])
df
: the name of the existing DataFrame;insert()
: the method used for adding new columns;column_index
: the position where the new column will be inserted (keep in mind that indexing starts at 0);column_name
: the name for the new column;[value_1, value_2, value_3]
: the values that will populate the new column.
Now we'll turn our attention to the countries
DataFrame and demonstrate how to add a new column named 'population'
, representing the populations of countries, right after the first column ('country'
).
12345678import pandas as pd countries_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(countries_data) countries.insert(1, 'population', [61399000, 75967000, 39244, 380200, 10380491, 5496000, 2424200]) print(countries)
Swipe to start coding
You are given a list named cars_data
.
- Given this list, create a
DataFrame
namedaudi_cars
. - Insert a column named
'price'
between the'year'
and'fueltype'
columns and populate this column with the following values:[12500, 16500, 16800, 17300, 13900]
.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 3.03Awesome!
Completion rate improved to 3.03
Inserting a New Column
We'll now explore another technique for adding a column to a DataFrame. This approach utilizes the insert()
method. A key benefit of the insert()
method is that it allows you to specify the position of the new column within the DataFrame.
df.insert(column_index, 'column_name', [value_1, value_2, value_3])
df
: the name of the existing DataFrame;insert()
: the method used for adding new columns;column_index
: the position where the new column will be inserted (keep in mind that indexing starts at 0);column_name
: the name for the new column;[value_1, value_2, value_3]
: the values that will populate the new column.
Now we'll turn our attention to the countries
DataFrame and demonstrate how to add a new column named 'population'
, representing the populations of countries, right after the first column ('country'
).
12345678import pandas as pd countries_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(countries_data) countries.insert(1, 'population', [61399000, 75967000, 39244, 380200, 10380491, 5496000, 2424200]) print(countries)
Swipe to start coding
You are given a list named cars_data
.
- Given this list, create a
DataFrame
namedaudi_cars
. - Insert a column named
'price'
between the'year'
and'fueltype'
columns and populate this column with the following values:[12500, 16500, 16800, 17300, 13900]
.
Solution
Thanks for your feedback!
single
Awesome!
Completion rate improved to 3.03
Inserting a New Column
Swipe to show menu
We'll now explore another technique for adding a column to a DataFrame. This approach utilizes the insert()
method. A key benefit of the insert()
method is that it allows you to specify the position of the new column within the DataFrame.
df.insert(column_index, 'column_name', [value_1, value_2, value_3])
df
: the name of the existing DataFrame;insert()
: the method used for adding new columns;column_index
: the position where the new column will be inserted (keep in mind that indexing starts at 0);column_name
: the name for the new column;[value_1, value_2, value_3]
: the values that will populate the new column.
Now we'll turn our attention to the countries
DataFrame and demonstrate how to add a new column named 'population'
, representing the populations of countries, right after the first column ('country'
).
12345678import pandas as pd countries_data = {'country' : ['Thailand', 'Philippines', 'Monaco', 'Malta', 'Sweden', 'Paraguay', 'Latvia'], 'continent' : ['Asia', 'Asia', 'Europe', 'Europe', 'Europe', 'South America', 'Europe'], 'capital':['Bangkok', 'Manila', 'Monaco', 'Valletta', 'Stockholm', 'Asuncion', 'Riga']} countries = pd.DataFrame(countries_data) countries.insert(1, 'population', [61399000, 75967000, 39244, 380200, 10380491, 5496000, 2424200]) print(countries)
Swipe to start coding
You are given a list named cars_data
.
- Given this list, create a
DataFrame
namedaudi_cars
. - Insert a column named
'price'
between the'year'
and'fueltype'
columns and populate this column with the following values:[12500, 16500, 16800, 17300, 13900]
.
Solution
Thanks for your feedback!