Q:

How to extract specific columns to new DataFrame?

belongs to collection: Python Pandas Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Pandas Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Why should we make a copy of a DataFrame in Pandas... >>
<< How to convert Pandas DataFrame to list of Diction...