user13339757
user13339757

Reputation:

error in pandas dataframe when trying to convert dtype object to float

I am trying to aggregate data converted from an xlsx file to a dataframe but it is all dtype object. I tried to convert it to strings and float so I can aggregate it but this is the error I get :

SystemError: <class 'UnicodeEncodeError'> returned a result with an error set

This is my code :

new_df['Date'] = pd.to_datetime(new_df['Date'])
new_df[['Country', 'City', 'Specie']] = new_df[['Country', 'City', 'Specie']].astype('|S80')
new_df[['count', 'min', 'max', 'median', 'variance']] = pd.to_numeric(new_df[['count', 'min', 'max', 'median', 'variance']],errors='coerce')

This is a sample of the dataframe :

         Date Country       City Specie count   min   max median variance
   2020-02-23      CR  San José   pm25    20  13.0  53.0   25.0  1232.00
   2020-04-04      CR  San José   pm25    23  17.0  57.0   38.0  1302.57
   2020-04-24      CR  San José   pm25    23  30.0  80.0   59.0  1966.13
   2020-01-14      CR  San José   pm25    24  13.0  34.0   21.0   379.55
   2020-02-07      CR  San José   pm25    23  57.0  95.0   72.0   838.97

Upvotes: 0

Views: 200

Answers (1)

rockandfish
rockandfish

Reputation: 11

should check how ".csv file" is encoded first.

If that csv file is encoded as"utf-8", try this

df = pd.read_csv(filename, encoding="utf-8")

Upvotes: 1

Related Questions