How to Create a New Dataframe With Selected Columns of Existing Dataframe in Python

To create a new dataframe with selected columns of an existing dataframe in Python, you can use the = assignment operator in combination with [] square brackets.

The following example shows how to create a new dataframe with selected columns of an existing dataframe in Python.

Using = Operator & [] Bracket

The combination of the = operator and [] brackets allows you to pick specific columns and create a new DataFrame.

Let’s start with a DataFrame containing various product details and filter specific columns:

# Import pandas library
import pandas as pd

# Create dataframe
old_df = pd.DataFrame({
    'Date': ['01-03-2023', '01-03-2023', '01-03-2023', '01-03-2023', '02-03-2023', '02-03-2023'],
    'Product_Code': ['A-101', 'A-102', 'A-103', 'B-101', 'B-102', 'B-104'],
    'Product_Name': ['Laptop', 'Mobile', 'Printer', 'Keyboard', 'Scanner', 'Mouse'],
    'Price': [4500, 550, 250, 50, 350, 50],
    'Status': [1, 1, 1, 0, 1, 1]
})

# Create new dataframe with selected columns
new_df = old_df[["Date", "Product_Code", "Price"]]

# Show new dataframe
print(new_df)

Output: 👇️

         Date Product_Code  Price
0  01-03-2023        A-101   4500
1  01-03-2023        A-102    550
2  01-03-2023        A-103    250
3  01-03-2023        B-101     50
4  02-03-2023        B-102    350
5  02-03-2023        B-104     50

In this code snippet:

  • old_df: The original DataFrame with all columns.
  • new_df: A new DataFrame that includes only the selected columns “Date”, “Product_Code”, and “Price”.
  • Square Brackets ([]): Used to specify the column names you want to extract.

The output shows the new dataframe with only the selected columns.

Conclusion

Selecting specific columns from a DataFrame in Python is straightforward and essential for efficient data manipulation.

  • Use the = assignment operator with [] square brackets to extract the columns you need.
  • This approach allows you to streamline your dataset for analysis or other tasks.