Integers and floats
Remember you can use the type() function to see the data type of an object?
However, this information is insufficient when working with DataFrames, since the result will be:
print(type(some_DataFrame))
<class 'pandas.core.frame.DataFrame'>
If you want to find out the data type of each column in the DataFrame, you can either use the .info() method or the .dtypes attribute. Now, if you want to change the data type of a column, you can call the .astype() method on the column along with the new type for the column. For example, to convert the type of 'column_a' to integer:
df['column_a'] = df['column_a'].astype(int)
Cet exercice fait partie du cours
Python for R Users
Instructions
- Inspect the output of
tips.dtypesin the shell. - Convert the
sizecolumn tointtype. - Convert the
tipcolumns tofloattype. - Look at
.dtypesagain.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# Convert the size column
tips['size'] = tips['size']____
# Convert the tip column
____ = ____
# Look at the types
print(____)