Mario
Mario

Reputation: 1966

How can fix "convert the series to <class 'float'>" problem in Pandas?

I've got a simple question which I couldn't understand why it is the case for me . I'm reading two matrices A and B (which are 24*20)and calculate subtraction matrix of them and save it then I calculate MAPE(Mean Absolute Percentage Error). The problem is when I read them via Pandas and apply following formula on subtraction matrix:

I've got production of this formula for each columns as 'series' :

0     2.252708
1     1.727362
2     1.928928
3     1.562168
4     2.015080
5     2.760333
6     1.497950
7     1.047574
8     1.078431
9     1.065895
10    1.159555
11    0.937553
12   -0.130836
13   -0.090051
14    0.919025
15    0.094861
16    0.839204
17   -0.880221
18   -1.571482
19   -0.400643
dtype: float64

while via Numpy I've got just one right answer.

17.813396179645633

Following is my code:

# Import and call the needed libraries
import numpy as np
import pandas as pd

#A = np.zeros((24,20))
#B = np.zeros((24,20))

A = pd.read_csv('D:\A.csv', header=None)
B = pd.read_csv('D:\B.csv', header=None)

#A = np.loadtxt('D:\A.csv', delimiter=',' )
#B = np.loadtxt('D:\B.csv', delimiter=',' )

delta1a = A - B


df_delta1a = pd.DataFrame(delta1a, index=None)
df_delta1a.to_csv(f'Subtraction_Matrix_1a_.csv', na_rep='nan', encoding='utf-8', index=False)


#MAPE formula
mape_plot_1a = 100 *( 1 - np.abs( ( delta1a) / A) )
mape_1a = 100 *( np.sum( 1 - np.abs( delta1a / A) ) )/480
print(mape_1a)
mape_percentage_1a = ("%.2f%%" % mape_1a)

So how can I read matrices via Pandas and get right unique result after applying formula? why Pandas return 'series' and consequently I face following error:

TypeError: cannot convert the series to <class 'float'>

I'm wondering if my case is converting from Pandas to Numpy arrays or another issue since I've checked there are some answers like this and that but I couldn't fix it.

Upvotes: 0

Views: 101

Answers (1)

furas
furas

Reputation: 142651

pandas works rather with rows or columns, not with matrix.

But you can sum values in your Series to get expected value

I took your values but they can be little rounded (pandas display little rounded values) so result is little different.

import pandas as pd

data = pd.Series([2.252708, 1.727362, 1.928928, 1.562168, 2.01508, 2.760333, 1.49795, 1.047574, 1.078431, 1.065895, 1.159555, 0.937553, -0.130836, -0.090051, 0.919025, 0.094861, 0.839204, -0.880221, -1.571482, -0.400643])

print(data.sum())

17.813394000000006

Upvotes: 1

Related Questions