Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Adding new Columns | Basics of Dataframes
Pandas: First Steps
course content

Contenuti del Corso

Pandas: First Steps

Pandas: First Steps

1. Getting Started
2. Basics of Dataframes
3. Fetching Data from DataFrames

book
Adding new Columns

We can easily add new columns to an existing DataFrame using the indexing syntax:

js

It is important that if values is a list, it needs to have the same number of elements as there are rows in the DataFrame, otherwise you'll get an error. Alternatively, we can assign a single value to the entire new column as well.

Example 1: New Column from a Single Value

1234567891011
import pandas as pd df = pd.DataFrame({ 'Product': ['Laptop', 'Tablet', 'Phone'], 'Old Price': [1000, 600, 400] }) # Add a new column with a fixed price for all rows df['New Price'] = 700 print(df)
copy

Example 2: New Column using a List of Values

1234567891011
import pandas as pd df = pd.DataFrame({ 'Member': ['Sophie', 'Liam', 'Emma'], 'Role': ['Designer', 'Developer', 'Tester'] }) # Add a new column for Project Name df['Project'] = ['Marketing Website Redesign', 'Client Portal Backend Development', 'Payment Flow QA Testing'] print(df)
copy

Example 3: New Column derived from Another Column

1234567891011
import pandas as pd df = pd.DataFrame({ 'Product': ['Laptop', 'Tablet', 'Phone'], 'Price': [1000, 600, 400] }) # Derive a new column 'Discounted Price' (10% off) df['Discounted Price'] = df['Price'] * 0.9 print(df)
copy

1. What happens if you try to add a new column to a DataFrame with a list that has fewer elements than the number of rows?

2. Which of the following lines correctly adds a column Discount which is 20% of the Price?

3. What would be the output of the following code?

question mark

What happens if you try to add a new column to a DataFrame with a list that has fewer elements than the number of rows?

Select the correct answer

question mark

Which of the following lines correctly adds a column Discount which is 20% of the Price?

Select the correct answer

question mark

What would be the output of the following code?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 5

Chieda ad AI

expand
ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

course content

Contenuti del Corso

Pandas: First Steps

Pandas: First Steps

1. Getting Started
2. Basics of Dataframes
3. Fetching Data from DataFrames

book
Adding new Columns

We can easily add new columns to an existing DataFrame using the indexing syntax:

js

It is important that if values is a list, it needs to have the same number of elements as there are rows in the DataFrame, otherwise you'll get an error. Alternatively, we can assign a single value to the entire new column as well.

Example 1: New Column from a Single Value

1234567891011
import pandas as pd df = pd.DataFrame({ 'Product': ['Laptop', 'Tablet', 'Phone'], 'Old Price': [1000, 600, 400] }) # Add a new column with a fixed price for all rows df['New Price'] = 700 print(df)
copy

Example 2: New Column using a List of Values

1234567891011
import pandas as pd df = pd.DataFrame({ 'Member': ['Sophie', 'Liam', 'Emma'], 'Role': ['Designer', 'Developer', 'Tester'] }) # Add a new column for Project Name df['Project'] = ['Marketing Website Redesign', 'Client Portal Backend Development', 'Payment Flow QA Testing'] print(df)
copy

Example 3: New Column derived from Another Column

1234567891011
import pandas as pd df = pd.DataFrame({ 'Product': ['Laptop', 'Tablet', 'Phone'], 'Price': [1000, 600, 400] }) # Derive a new column 'Discounted Price' (10% off) df['Discounted Price'] = df['Price'] * 0.9 print(df)
copy

1. What happens if you try to add a new column to a DataFrame with a list that has fewer elements than the number of rows?

2. Which of the following lines correctly adds a column Discount which is 20% of the Price?

3. What would be the output of the following code?

question mark

What happens if you try to add a new column to a DataFrame with a list that has fewer elements than the number of rows?

Select the correct answer

question mark

Which of the following lines correctly adds a column Discount which is 20% of the Price?

Select the correct answer

question mark

What would be the output of the following code?

Select the correct answer

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 5
Siamo spiacenti che qualcosa sia andato storto. Cosa è successo?
some-alt