Home > database >  DataFrame.loc results in string objects
DataFrame.loc results in string objects

Time:06-01

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'.

Thanks!

CodePudding user response:

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

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