0

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

1 Answer 1

1

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

tuple(df.loc[1.0, ['A','B']].astype(float))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.