drop a list of columns from dataframe pandas code example
Example 1: python - dataframe columns is a list - drop
key1 = 'site channel fiscal_week'.split()
key2 = 'site dude fiscal_week'.split()
key3 = 'site eng fiscal_week'.split()
keys = pd.DataFrame({'key': [1,2,3],
'dims': [key1,key2,key3]})
dims key
[site, channel, fiscal_week] 1
[site, dude, fiscal_week] 2
[site, eng, fiscal_week] 3
keys['reduced_dims'] = keys['dims'].apply(
lambda row: [val for val in row if val != 'fiscal_week']
)
Example 2: drop dataframe columns from list of column names
df.drop(cat_columns, axis = 1, inplace = True)
dict_1 = {'workclass_stripped':'workclass', 'education_stripped':'education',
'marital-status_stripped':'marital_status', 'occupation_stripped':'occupation',
'relationship_stripped':'relationship', 'race_stripped':'race',
'sex_stripped':'sex', 'native-country_stripped':'native-country',
'Income_stripped':'Income'}
df.rename(columns = dict_1, inplace = True)
df