0

I'm learning python for Data science on my own. I've done some research and I can't seem to figure out whats going on with my Code to cause this. Here's my code:

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sb
from pylab import rcParams

%matplotlib inline
rcParams['figure.figsize'] = 5,4
sb.set_style('whitegrid')

df = pd.read_csv(filepath_or_buffer='/Users/personal/Desktop/Data set/churn_raw_data.csv', header=None, sep=',')
df.columns=['Customer_id','Age', 'Income', 'Outage_sec_perweek','Contacts, Yearly_equip_failure', 'Tenure', 'MonthlyCharge', 'Bandwidth_GB_year']

The issue in the code is with the df.columns line. It's telling me this error: ValueError: Length mismatch: Expected axis has 52 elements, new values have 8 elements.

I know that the issue has something to do with my header. I tried to use 'none" but this doesnt seem to solve my issue. Its as if it wants to me to call on all 52 columns, which I dont want to do. Any help would be amazing.

BrOtis
  • 1
  • 2
  • Please read [mre]. It should always include minimal example of any data - maybe a few lines of the csv file contents. [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – wwii Feb 13 '21 at 17:43
  • Did you search with the error message - `pandas ValueError: Length mismatch: Expected axis has 52 elements, new values have 8 elements.` - reading the results will give you an idea of where to start. – wwii Feb 13 '21 at 17:47

1 Answers1

0

I suspect has nothing to do with matplotlib. What you're looking for is creating a view/copy on a subset of columns of a given pandas.DataFrame:

import pandas as pd

# read dataframe normally
df = pd.read_csv(filepath_or_buffer='/Users/personal/Desktop/Data set/churn_raw_data.csv', header=None, sep=',')

# select which columns to keep
# optionally create a copy as well to not just work with a view on the big (52 col) DataFrame
df = df[['Customer_id','Age', 'Income', 'Outage_sec_perweek','Contacts, Yearly_equip_failure', 'Tenure', 'MonthlyCharge', 'Bandwidth_GB_year']].copy()

Never hurts to have a look at pandas indexing as well.

Stefan B
  • 1,617
  • 4
  • 15
  • I called myself looking at that. lol. Im still learning how to research issues like this. I really appreciate you helping a noob like me. I've tried to implement what you suggested above and im still getting the error. – BrOtis Feb 13 '21 at 18:11