Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

python - Unable to drop columns from a pandas dataframe

I am trying to drop a column from a pandas dataframe as follows:

df = pd.read_csv('Caravan_Dataset.csv')
X = df.drop('Purchase',axis=1)
y = df['Purchase'] 

but it does not work. I also tried the following one:

df = pd.read_csv('Caravan_Dataset.csv')
X = df.drop('Purchase',axis=1,inplace=True)
y = df['Purchase']

but it does not work neither. ?t keeps giving an error like Purchase is still on the columns. Any idea about how can I do it?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When inplace = True , the data is modified in place, which means it will return nothing and the dataframe is now updated. When inplace=False, you will need to assign it to something new.

Change your code from:

X = df.drop('Purchase',axis=1,inplace=True)

To this:

df.drop('Purchase',axis=1,inplace=True)

Or, alternatively use inplace=False (which is the default) and returns a copy of the object, and use:

X = df.drop('Purchase',axis=1)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...