Add a New Column 2/2
Let's explore the second technique for adding a column to a DataFrame. This method utilizes the insert()
function. Here's the syntax:
dataframe
: The name of the existing DataFrame;insert()
: The function 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 let's turn our attention to the countries
DataFrame. We'll demonstrate how to add a new column named population
, representing the populations of countries, right after the first column (country
).
Note
A key benefit of the
insert()
method is that it allows you to specify the position of the new column within the DataFrame.
Tarea
- Using the
dataset
data, create theaudi_cars
DataFrame. - Insert a column named
'price'
between theyear
andfueltype
columns. Use the.insert()
method and populate it with the following values:[12500, 16500, 16800, 17300, 13900]
.
¿Todo estuvo claro?
Contenido del Curso
Pandas First Steps
2. Reading Files in Pandas
3. Analyzing the Data
Pandas First Steps
Add a New Column 2/2
Let's explore the second technique for adding a column to a DataFrame. This method utilizes the insert()
function. Here's the syntax:
dataframe
: The name of the existing DataFrame;insert()
: The function 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 let's turn our attention to the countries
DataFrame. We'll demonstrate how to add a new column named population
, representing the populations of countries, right after the first column (country
).
Note
A key benefit of the
insert()
method is that it allows you to specify the position of the new column within the DataFrame.
Tarea
- Using the
dataset
data, create theaudi_cars
DataFrame. - Insert a column named
'price'
between theyear
andfueltype
columns. Use the.insert()
method and populate it with the following values:[12500, 16500, 16800, 17300, 13900]
.
¿Todo estuvo claro?