convert dataframe from int data type to float in python

To convert a column in a pandas DataFrame from int data type to float, you can use the astype method. Here's an example:

main.py
import pandas as pd

# Create a sample dataframe
df = pd.DataFrame({'int_column': [1, 2, 3]})

# Convert the 'int_column' from int to float
df['float_column'] = df['int_column'].astype(float)

# Print the converted dataframe
print(df)
235 chars
11 lines

This will output:

main.py
   int_column  float_column
0           1           1.0
1           2           2.0
2           3           3.0
112 chars
5 lines

In the example above, the astype(float) method is used to convert the 'int_column' from int data type to float data type. The converted column is then assigned to a new column 'float_column' in the DataFrame.

Note that the original 'int_column' is still present in the DataFrame, and the converted values are stored in the 'float_column'.

gistlibby LogSnag