Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading