How to Create a New dataframe with selected columns : Methods

Create a New dataframe with selected columns

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 new pandas 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

Creating new dataframe from old dataframe using the copy() function
Creating a new dataframe from an old dataframe using the copy() function

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)
Output
Creating new dataframe from old dataframe using the filter function
Creating new dataframe from old dataframe using the filter function

Method 3: Remove the columns

The third method to create a new dataframe from the existing one is the use of drop() function. Here you have to drop the columns that you don’t want in the new dataframe.
new_df = df[df.columns.drop(["col2"])]
Execute the below lines of code to remove the columns and choose the specified ones.
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)
Output
Creating new dataframe from old dataframe using the drop function
Creating new dataframe from old dataframe using the drop function

Conclusion

Selecting some columns from the existing dataframe is done to make computational work faster on the new one. It is due to less usage of memory. These are the method to create a new dataframe with columns. You can choose from three methods copy(), filter(), or drop() method to achieve that.
I hope this article has cleared your mind on how to extract a new dataframe with the selected columns. If you have any other methods for this problem then you can contact us for adding here. You can also contact us for more queries.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner