0

I am having an issue with pandas reading my data file. The relevant code I'm using at the moment is this:

import pandas as pd

file_data = pd.read_csv('14_May_2022.csv')
complete_df = pd.DataFrame(file_data)
print(complete_df.info())
Current_Players_df = complete_df[["Game","Current_Players"]]
print(Current_Players_df)
print("The average current players is:\n")
print(Current_Players_df["Current_Players"].mean())`

when I try to run this all is fine until the last line of code where I get a lengthy error code beginning with:

Traceback (most recent call last):
  File "C:\Users\christy\AppData\Roaming\Python\Python37\site-packages\pandas\core\nanops.py", line 1603, in _ensure_numeric
    x = float(x)
ValueError: could not convert string to float:

and then going on to list every value (which are all numbers)


I have tried two possible solutions but neither work. These are the ones I've tried:

complete_df['Current_Players']= pd.to_numeric(complete_df['Current_Players'].astype(str).str.strip(),error='coerce')

The error message shows:

Traceback (most recent call last):
  File "C:\Users\christy\OneDrive\Documents\steam data analysis\analysis.py", line 6, in <module>
    complete_df['Current_Players']= pd.to_numeric(complete_df['Current_Players'].astype(str).str.strip(),error='coerce')
TypeError: to_numeric() got an unexpected keyword argument 'error'
str.strip(complete_df['Current_Players'])

The error message shows:

Traceback (most recent call last):
  File "C:\Users\christy\OneDrive\Documents\steam data analysis\analysis.py", line 7, in <module>
    str.strip(complete_df['Current_Players'])
TypeError: descriptor 'strip' requires a 'str' object but received a 'Series'
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
  • 1
    [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). Please read and include a [mre]. – wwii May 17 '22 at 19:43
  • **Show us your data**. Show us the offending line of CSV file. If pd.read_csv infers it as string not numeric, there must be some whitespace, punctuation, other symbol and/or units causing it to be inferred as string. So show us that data. – smci May 18 '22 at 01:50

1 Answers1

1

First off, it may be hard for us to help without having your data.

That said, in your pd.to_numeric() call, the keyword is errors with an 's', you just have error. Fixing that might get you somewhere.

Also, why do you have your second line calling pd.DataFrame(file_data)? The first line already created a dataframe - did you find that things weren't working without this? I expect that that line is making no change - or perhaps even making a change for the worse.

scotscotmcc
  • 2,719
  • 1
  • 6
  • 29