0
0
Pandasdata~15 mins

Selecting columns by name in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Selecting columns by name
📖 Scenario: You work in a small shop and keep track of sales data in a table. You want to look at only some parts of the data to understand sales better.
🎯 Goal: Learn how to select specific columns by their names from a table of data using pandas.
📋 What You'll Learn
Create a pandas DataFrame with given sales data
Create a list of column names to select
Select columns from the DataFrame using the list of column names
Print the selected columns
💡 Why This Matters
🌍 Real World
Selecting specific columns helps you focus on important parts of your data, like sales numbers or product names, without extra details.
💼 Career
Data scientists often select columns by name to prepare data for analysis or visualization, making their work clearer and faster.
Progress0 / 4 steps
1
Create the sales data table
Create a pandas DataFrame called sales_data with these exact columns and rows:
Product: ['Apple', 'Banana', 'Carrot', 'Dates']
Price: [1.2, 0.5, 0.8, 3.0]
Quantity: [10, 20, 15, 7]
Date: ['2024-06-01', '2024-06-01', '2024-06-02', '2024-06-02']
Pandas
Need a hint?

Use pd.DataFrame with a dictionary where keys are column names and values are lists of column values.

2
Create a list of columns to select
Create a list called columns_to_select with these exact column names: 'Product' and 'Quantity'
Pandas
Need a hint?

Use square brackets to create a list with the exact column names as strings.

3
Select columns from the DataFrame
Create a new DataFrame called selected_data by selecting the columns in columns_to_select from sales_data
Pandas
Need a hint?

Use square brackets with the list of column names to select columns from the DataFrame.

4
Print the selected columns
Print the selected_data DataFrame to see only the selected columns
Pandas
Need a hint?

Use print(selected_data) to show the selected columns.