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]
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 as pd
data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],
'Age':[27, 24, 22, 32],
'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],
'Qualification':['Msc', 'MA', 'MCA', 'Phd']}
df = pd.DataFrame(data)
df[['Name', 'Qualification']]