deadcode
deadcode

Reputation: 782

pandas.drop() not dropping columns

I am working on the Allstate data from Kaggle. I am looping through the categorical columns to create dummies but the pd.drop() is not dropping my columns. I have tried

for i in range(44):
    dummies1 = pd.get_dummies(combined[cols_cat[i+71]], prefix=cols_cat[i+72])
    combined = pd.concat([combined,dummies1], axis=1)
    combined = combined.drop(combined[cols_cat[i+71]], axis=1)

or

for i in range(44):
    dummies1 = pd.get_dummies(combined[cols_cat[i+71]], prefix=cols_cat[i+72])
    combined = pd.concat([combined,dummies1], axis=1)
    combined.drop(combined[cols_cat[i+71]], axis=1, inplace=True)

but when I check combined.head(), the dummies are created alright but the original columns still stay. The drop function has always given me trouble and I feel like I have still failed to understand the inplace part of the drop function after much use/research. Here, cols_cat is an array of the column names I created earlier.

cols_cat = train.iloc[:,1:117].columns
cols_cont = train.iloc[:,117:131].columns 

I am working on creating a model dataframe for people to try on so that this is easier to answer. Until then does someone spot any fundamental mistake I am making?

Upvotes: 0

Views: 5368

Answers (1)

Bhushan Pant
Bhushan Pant

Reputation: 1580

Do this:

combined = combined.drop(cols_cat[i+71], axis=1)

OR

combined.drop(cols_cat[i+71], axis=1, inplace=True)

As per pandas documentation, axis=1 drops labels from columns, while axis=0 drops labels from index.

Upvotes: 1

Related Questions