Extracting Multiple Columns
We can fetch a single column from a DataFrame using the following syntax:
DataFrame['column name']
Note
When we fetch a single column, it is fetched in the form of a Series. Hence, all the methods and properties of a series are applicable to it.
It's possible to extract multiple columns from a DataFrame using the following syntax:
DataFrame[[column1, column2, ...]]
Where column1
, column2
, and so on, represent the names of the columns in string format.
The following example demonstrates the usage:
12345678910111213141516import pandas as pd # Create a sample DataFrame with sales data df = pd.DataFrame({ 'Order ID': [101, 102, 103, 104], 'Customer Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Amount Paid': [50.0, 30.0, 75.0, 40.0], 'Product': ['Laptop', 'Phone', 'Tablet', 'Monitor'], 'Purchase Date': ['2025-04-01', '2025-04-01', '2025-04-01', '2025-04-01'] }) # Extract specific columns from the DataFrame selected_columns = df[['Order ID', 'Customer Name', 'Amount Paid']] # Print the extracted columns print(selected_columns)
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Ställ mig frågor om detta ämne
Sammanfatta detta kapitel
Visa verkliga exempel
Awesome!
Completion rate improved to 2.7
Extracting Multiple Columns
Svep för att visa menyn
We can fetch a single column from a DataFrame using the following syntax:
DataFrame['column name']
Note
When we fetch a single column, it is fetched in the form of a Series. Hence, all the methods and properties of a series are applicable to it.
It's possible to extract multiple columns from a DataFrame using the following syntax:
DataFrame[[column1, column2, ...]]
Where column1
, column2
, and so on, represent the names of the columns in string format.
The following example demonstrates the usage:
12345678910111213141516import pandas as pd # Create a sample DataFrame with sales data df = pd.DataFrame({ 'Order ID': [101, 102, 103, 104], 'Customer Name': ['Alice', 'Bob', 'Charlie', 'David'], 'Amount Paid': [50.0, 30.0, 75.0, 40.0], 'Product': ['Laptop', 'Phone', 'Tablet', 'Monitor'], 'Purchase Date': ['2025-04-01', '2025-04-01', '2025-04-01', '2025-04-01'] }) # Extract specific columns from the DataFrame selected_columns = df[['Order ID', 'Customer Name', 'Amount Paid']] # Print the extracted columns print(selected_columns)
Tack för dina kommentarer!