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)
This exercise is part of the course
Python for R Users
Exercise instructions
- Inspect the output of
tips.dtypes
in the shell. - Convert the
size
column toint
type. - Convert the
tip
columns tofloat
type. - Look at
.dtypes
again.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Convert the size column
tips['size'] = tips['size']____
# Convert the tip column
____ = ____
# Look at the types
print(____)