extract columns from dataframe pandas code example

Example 1: python - subset specific columns name in a dataframe

columns = ['b', 'c']
df1 = pd.DataFrame(df, columns=columns)

Example 2: select columns pandas

df1 = df.iloc[:,0:2] # Remember that Python does not slice inclusive of the ending index.

Example 3: extract one column from dataframe python

import pandas as pd

input_file = "C:\\....\\consumer_complaints.csv"
dataset = pd.read_csv(input_file)
df = pd.DataFrame(dataset)
cols = [1,2,3,4]
df = df[df.columns[cols]]

Example 4: python pandas selecting multiple columns

# Import pandas package 
import pandas as pd 
  
# Define a dictionary containing employee data 
data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'], 
        'Age':[27, 24, 22, 32], 
        'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'], 
        'Qualification':['Msc', 'MA', 'MCA', 'Phd']} 
  
# Convert the dictionary into DataFrame  
df = pd.DataFrame(data) 
  
# select two columns 
df[['Name', 'Qualification']]