pandas get certain columns code example

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

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

Example 2: get certain columns pandas with string

import pandas as pd

data = {'spike-2': [1,2,3], 'hey spke': [4,5,6], 'spiked-in': [7,8,9], 'no': [10,11,12]}
df = pd.DataFrame(data)

spike_cols = [col for col in df.columns if 'spike' in col]
print(list(df.columns))
print(spike_cols)

Example 3: python: select specific columns in a data frame

df = df[["Column_Name1", "Column_Name2"]]

Example 4: select columns pandas

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

Example 5: how to get only certain columns in pandas

#only the column names specified will be put into the new sub df
#enter a minimum of one column name
print(df[['column_name_1', 'column_name_2', ... , 'column_name_n']])

Example 6: python pandas return column name of a specific column

# Basic syntax:
list(df.columns)[column_number]
# This returns the column name of the column of interest

Tags:

Sql Example