ADVERTISEMENT
ADVERTISEMENT

How to extract specific columns to new DataFrame?

Given a Pandas DataFrame, we have to extract specific columns to new DataFrame.
Submitted by Pranit Sharma, on May 18, 2022

Columns are the different fields that contains their particular values when we create a DataFrame. We can perform certain operations on both rows & column values. In pandas, we can make a copy of some specific columns of an old DataFrame. This is an easy task in pandas. Let us understand with the help of an example.

To work with pandas, we need to import pandas package first, below is the syntax:

import pandas as pd

Let us understand with the help of an example.

# Importing Pandas package
import pandas as pd

# Create a dictionary
d = {
    'A':['One','Two','Three'],
    'B':['Four','Five','Six'],
    'C':['Seven','Eight','Nine'],
    'D':['Ten','Eleven','Twelve']
}

# Create DataFrame
df1 = pd.DataFrame(d)

# Display DataFrame
print("Original DataFrame:\n",df1,"\n")

# Now converting another DataFrame consisting of 
# some specific columns of DataFrame 1
df2 = df1[['A','B','D']]

# Display New DataFrame
print("New DataFrame:\n",df2)

Output:

Example: Extract specific columns to new DataFrame

Python Pandas Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.