K K
K K

Reputation: 75

dropped column in pandas reappearing

I am trying to convert a pandas dataframe to a recarray after dropping a column. The original data has 33 columns and after dropping column 'a', I am left with 32 columns as expected. However, after calling to_records() on the dataframe, the original column has been reinserted with blank values. Is there a way to prevent this?

dat = pd.read_csv("testing.csv")
dat = dat.astype("float32")
dat.drop(['a'], axis=1, inplace=True)

temp = dat.to_records()

>>> dat.shape
(500,32)
>>> temp.shape
(500,33)

Upvotes: 2

Views: 6000

Answers (2)

Mahfuzullah Galib
Mahfuzullah Galib

Reputation: 25

just do dat.drop(['a'], axis=1, inplace=true) instead of dat=dat.drop(['a'], axis=1, inplace=true)

When inplace=True is passed, the data is renamed in place (it returns nothing), so you get a none type object in dat,

When inplace=False is passed (this is the default value, so isn't necessary), performs the operation and returns a copy of the object

also see this

Upvotes: 1

Imran
Imran

Reputation: 146

You are dropping the column as expected but you have to assign the new data frame to your original data frame so it's overwritten. So,

dat=dat.drop(['a'], axis=1, inplace=true)

That's it

Upvotes: 2

Related Questions