Suppose you have an existing dataframe with an “n ” number of columns. And if you want to create a new dataframe with the selected columns then how you will do so? In this tutorial, you will learn how to create a new dataframe from an existing dataframe with selected columns using various methods.
Sample Dataframe
Let’s first create a sample dataframe that will be used to extract specific columns from the existing dataframe and create a new one.
Execute the below lines of code to create it.
import pandas as pd
data = {"col1":[10,20,30,40,50],"col2":[1,2,3,4,5],"col3":[11,12,13,14,15],"col4":[110,120,130,140,150]}
df = pd.DataFrame(data)
print(df)
Output
col1 col2 col3 col4 0 10 1 11 110 1 20 2 12 120 2 30 3 13 130 3 40 4 14 140 4 50 5 15 150
Method to create a new dataframe with selected columns
Let’s know all the methods to create a new dataframe from the old dataframe with the selected columns.
Method 1: Using the copy() function
The first method is to copy the old datframe with the specific columns. Just choose the columns list inside the square bracket and copy it.
new_df = df[["col1","col3","col4"]].copy()
import pandas as pd
data = {"col1":[10,20,30,40,50],"col2":[1,2,3,4,5],"col3":[11,12,13,14,15],"col4":[110,120,130,140,150]}
df = pd.DataFrame(data)
new_df = df[["col1","col3","col4"]].copy()
print(new_df)
Output

Method 2: Create a new dataframe with selected columns using the filter() function
The second to create a new dataframe is by using the filter() function. Here you have to just pass the columns you want to extract and set the value of the axis to 1.
new_df = df.filter(["col1","col3","col4"],axis=1)
Run the below lines of code and see the output.
import pandas as pd
data = {"col1":[10,20,30,40,50],"col2":[1,2,3,4,5],"col3":[11,12,13,14,15],"col4":[110,120,130,140,150]}
df = pd.DataFrame(data)
new_df = df.filter(["col1","col3","col4"],axis=1)
print(new_df)
Method 3: Remove the columns
new_df = df[df.columns.drop(["col2"])]
import pandas as pd
data = {"col1":[10,20,30,40,50],"col2":[1,2,3,4,5],"col3":[11,12,13,14,15],"col4":[110,120,130,140,150]}
df = pd.DataFrame(data)
new_df = df[df.columns.drop(["col2"])]
print(new_df)
Conclusion
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.