Week 11: Error Converting entire dataFrame to int64

Hi,
Instructor used pd.to_numeric() as finction to convert objects to numbers.
He used ->
df.apply(pd.to_numeric)

to convert entire dataframe objects into numbers. Now when we use df.astype(){another function to convert object into number} we cannot directly write df.apply(df.astype()) as df.astype() requires a column name and it will show this error ->

We have to traverse through all columns but in order to do that, we have to convert dataframe into list and so .astype() cannot be applied.

How can I convert all df objects into numbers using df.astype() ?
PS: I don’t want to do this ->
df.astype({‘col1’ : ‘int32’ , ‘col2’ : ‘int32’ , … } )

Hi @sohammafidar2000,
If I understood it right, you want to apply df.astype() using df.apply(), and pass an argument with df.astype.
To pass arguments while calling a function using df.apply(), we can do it this way:

new_df = df.apply(df.astype, 'int64')

Please let me know if this gets you some error.

@Ishvinder Yes, you understood me correctly. Although the error which pops up is:-

1 Like

Try this:
df = df.astype('int64')

Example:

1 Like