Can you show me how to select multiple columns?

QuestionsQuestions8 SkillsProData Selection in PandasNov, 01 2025
083

Certainly! To select multiple columns from a DataFrame in Python using pandas, you can use a list of column names within the selection brackets []. Here's an example:

import pandas as pd

# Sample DataFrame
data = {
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'Sex': ['F', 'M', 'M']
}

titanic = pd.DataFrame(data)

# Select the 'Age' and 'Sex' columns
age_sex = titanic[['Age', 'Sex']]

# Display the first 5 rows
print(age_sex.head())

This code will create a DataFrame and then select the 'Age' and 'Sex' columns from it.

0 Comments

no data
Be the first to share your comment!