When trying to drop a column from my dataset using pandas, i get the error "['churn'] not found in axis"

Stack Overflow Asked by Utku Şenel on January 3, 2022

I want the x to be all the columns except "churn" column.
But when i do the below i get the "[‘churn’] not found in axis" error, eventhough i can see the column name when i write "print(list(df.column))"
Here is my code:

import pandas as pd
import numpy as np

df = pd.read_csv("/Users/utkusenel/Documents/Data Analyzing/data.csv", header=0)
x = df.drop(["churn"], axis=1) ## this is the part it gives the error

I am adding a snippet of my dataset as well:


One Answer

I see that your df snippet is separeted with ';' (semi colon). If that is what your actual data looks like, then probably your csv is being read wrong. Please try adding sep=';' to read_csv function.

df = pd.read_csv("/Users/utkusenel/Documents/Data Analyzing/data.csv", header=0, sep=';')

I also suggest print df.columns again and check if there is a leading or trailing whitespace in the column name for churn.

Answered by Farid Jafri on January 3, 2022

Add your own answers!

Ask a Question

Get help from others!

© 2024 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP