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

Dheeraj picture Dheeraj · Jul 21, 2017 · Viewed 11k times · Source

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.

Answer

piRSquared picture piRSquared · Jul 21, 2017

Use loc

normal_sum.loc[date, 'KWH']

See @MaxU's answer for at


Also get_value

normal_sum.get_value(date, 'KWH')

To return zero when date isn't in the index, you can

normal_sum.KWH.get(date, 0)