How to convert a series of one value to float only?

Viewed 24346

I have a series which has only one value and i want to get that value only. I ran a code to get t he value by index matching and i got a series like this:

(normal_sum['KWH'][(normal_sum['KWH'].index == date)])

Timestamp
2017-04-02    2934.93
Freq: D, Name: KWH, dtype: float64

But when i tried to convert it into a float by this:

float(normal_sum['KWH'][(normal_sum['KWH'].index == date)])

It is throwing an error:

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

Expected output: 2934.93

Any help would be appreciated.

There is an edit:

I am facing another problem:

Suppose i get an empty series then how can i convert it to zero.

i did this:

(normal_sum['KWH'][(normal_sum['KWH'].index == date)])

Got a series like this:

Series([], Freq: D, Name: KWH, dtype: float64)

please help.

4 Answers

if there is only one element in the data series, you could convert it to list by calling tolist() and taking the first element of the new list by referencing [0].

Related