AnswerBun.com

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)
print(df.head())
print(df.columns)
print(len(df.columns))
x = df.drop(["churn"], axis=1) ## this is the part it gives the error

I am adding a snippet of my dataset as well:

account_length;area_code;international_plan;voice_mail_plan;number_vmail_messages;total_day_minutes;total_day_calls;total_day_charge;total_eve_minutes;total_eve_calls;total_eve_charge;total_night_minutes;total_night_calls;total_night_charge;total_intl_minutes;total_intl_calls;total_intl_charge;number_customer_service_calls;churn;
1;KS;128;area_code_415;no;yes;25;265.1;110;45.07;197.4;99;16.78;244.7;91;11.01;10;3;2.7;1;no
2;OH;107;area_code_415;no;yes;26;161.6;123;27.47;195.5;103;16.62;254.4;103;11.45;13.7;3;3.7;1;no
3;NJ;137;area_code_415;no;no;0;243.4;114;41.38;121.2;110;10.3;162.6;104;7.32;12.2;5;3.29;0;no

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!

Related Questions

Red box isn’t placed as expected

1  Asked on November 4, 2021 by liya-thomas

       

String Comparison Javascript

2  Asked on November 4, 2021 by crimsonpython24

       

es5 using promise but returns defered before resolving

2  Asked on November 4, 2021 by rodney-wormsbecher

     

2 arrays with json problem (Node.js, Discord.js)

3  Asked on November 4, 2021 by mr-code

     

Is there a XAML equivalent to nameof?

2  Asked on November 4, 2021 by luxun

       

Multistage builds with yum

3  Asked on November 4, 2021 by vorticity

 

WooCommerce can’t see product variations

1  Asked on November 4, 2021 by tizio-fittizio

       

VScode setup with ROS and auto complete

3  Asked on November 4, 2021 by dixcipuli

         

Pick an account after Connect-MicrosoftTeams

4  Asked on November 4, 2021 by mauek-unak

   

Show local image file:///tmp/someimage.jpg

1  Asked on November 4, 2021 by dani-herrera

   

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir