ComeçarComece de graça

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)

Este exercício faz parte do curso

Python for R Users

Ver curso

Instruções do exercício

  • Inspect the output of tips.dtypes in the shell.
  • Convert the size column to int type.
  • Convert the tip columns to float type.
  • Look at .dtypes again.

Exercício interativo prático

Experimente este exercício completando este código de exemplo.

# Convert the size column
tips['size'] = tips['size']____

# Convert the tip column
____ = ____

# Look at the types
print(____)
Editar e executar o código