Home > OS >  Specifying astype() for reading columns of a dataframe
Specifying astype() for reading columns of a dataframe

Time:02-21

There is a dataframe with two columns where the first is an integer number and the second is a float number:

       inst    weight
0    126400  0.845194
1   4382592  0.083728

If I use row = df.iloc[0].astype(float) then the first column is also converted to float. I would like to statically specify int and float and assign them to variables, e.g.:

w = df['weight'].iloc[0].astype(float)
i = df['inst'].iloc[0].astype(int)

But that results in an error like AttributeError: 'float' object has no attribute 'astype'. How can I fix that?

CodePudding user response:

You have to set the whole column as the type.

Like:

df['weight'] = df['weight'].astype(float)
  • Related