EngGu
EngGu

Reputation: 489

DataFrame.loc results in string objects

I have a dataframe as shown below.

df = 

        A           B
        
1.0     4.0         45.0
2.0     4.0         11.2
3.0     49.2        10.8

When I select a particular row using loc:

df.loc[1.0][['A','B']]

The above line of code results in the following:

A    4.0
B    45.0
Name: 1.0, dtype: object

I want to represent these values in a tuple. Therefore, I do the following:

tuple(df.loc[1.0][['A','B']])

which gives me a result ('4.0', '45.0') which indicates the values inside the tuple are of type string. How do I convert it to float? That is I want the result to look like (4.0, 45.0).

I tried to convert the value before representing it as tuple. For example : df.loc[1.0]['A'].astype(float64) and it resulted in the following error. AttributeError: 'str' object has no attribute 'astype'.

Upvotes: 0

Views: 328

Answers (1)

jezrael
jezrael

Reputation: 862521

Because df.loc[1.0, ['A','B']] is Series is possible change values to floats:

tuple(df.loc[1.0, ['A','B']].astype(float))

Upvotes: 1

Related Questions