Pandas DataFrame Data Types

Use cases for code example

Use case examples – Pandas DataFrame data types

Pandas DataFrame stores different types of data in each column of data. Each column of data will contain rows of records of the same data type. Here are the data types available for Pandas DataFrames.

You can list the data types of a dataframe using the command df.dtypes

Data Type in Pandas TypeUsage
objectstrText / String
int64intInteger / whole numbers
float64floatNumbers with decimal places
boolboolTrue/False values
datetime64NADate and time values
timedelta[ns]NADifferences between two datetimes
categoryNAFinite list of text values

Walkthrough and explanation of code

Walkthrough of code – Define datatype for Pandas Dataframe column field

The following code provides examples of how to define the following data types for a particular column field:

  • define column field as float data type
  • define column field as string data type
  • define column field as boolean

Example code

Example Code – Define datatype for Pandas Dataframe column field

 

# set column fieldname in dataframe df to float 
df['fieldname'].astype('float')

# set column fieldname in dataframe df to string 
df['fieldname'].astype('str')

# set column fieldname in dataframe df to boolean 
df['fieldname'].astype('bool')

Was this article helpful?

Related Articles