How can I convert a series of one value to float?

I have a series that only has one value and I only want to get that value. I ran the code to get the value by index 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 to float like this:

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

      

Throws an error:

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

      

Expected Result: 2934.93

Any help would be appreciated.

There is editing:

I faced 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)])

      

It turns out such a series:

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

      

Please, help.

+3


source to share


3 answers


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 if the date is not in the index, you can

normal_sum.KWH.get(date, 0)

      

+3


source


we can use Series.at [...] scalar search method :



In [138]: normal_sum = pd.Series([1.234], index=['KWH'])

In [139]: normal_sum
Out[139]:
KWH    1.234
dtype: float64

In [140]: normal_sum.at['KWH']
Out[140]: 1.234

      

+1


source


That being said, you are trying to convert the series to a float, which is not possible. Potentially, a series can have multiple records, and each of those records should not be floating point or integer, it could be anything. Thus, you have to select your specific entry using (bad way):

normal_sum['KWH'].loc[0]

      

or

normal_sum['KWH'].iloc[date]

      

Edit: Indexing the chain should be avoided as it was before, as follows.

If you select the dataframe form directly (and not from the normal_sum ['KWH'] series) you can simply do:

normal_sum.iloc[0,0]

      

or

normal_sum.loc[date, 'KWH']

      

0


source







All Articles