I have been trying to build a preprocessing pipeline, but I am struggling a little to generate a list of the indexes for each column that is an object dtype. I have been able to get the names of each into an array using the following code:
categorical_features = [col for col in input.columns if input[col].dtype == 'object']
Is there an easy way to get the index of these columns, from the original input dataframe into a list, like this one that I built manually?
c = [1,3,4,5,6,7,8,9,10,11,12,14,15,16,17,18,19,20,21,22,23,24,25,28,29,
30,31,38,39,40,41,42,43,44,45,50,51,55,56]
CodePudding user response:
I think you need select.dtypes
and enumerate
df = pd.DataFrame({'A' : ['A', 'B', 'C'], 'B' : [1,2,3], 'C' : [1, '2', '3']})
print(df)
A B C
0 A 1 1
1 B 2 2
2 C 3 3
idx_cols = [idx for idx, col in enumerate(df.select_dtypes('object').columns) ]
[0, 1]
CodePudding user response:
enumerate
can help with that:
categorical_features_indexes = [i for i, col in enumerate(input.columns) if input[col].dtype == 'object']
CodePudding user response:
Use df.select_dtypes
df.columns.get_indexer
:
categorical_features = df.columns.get_indexer(df.select_dtypes('object').columns)
df.select_dtypes
returns a copy ofdf
with only the columns that are of the specified dtype(s) (you can specify multiple, e.g.df.select_dtypes(['object', 'int'])
).df.columns.get_indexer
returns the indexes of the specified columns.