• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / Pandas DataFrame / Change data types of columns / How to Change Type for One or More Columns in Pandas Dataframe?

How to Change Type for One or More Columns in Pandas Dataframe?

September 28, 2018 by cmdlinetips

Sometimes when you create a data frame, some of the columns may be of mixed type. And you might see warning like this

DtypeWarning: Columns (0) have mixed types. Specify dtype option on import or set low_memory=False.

We get this error when Pandas tries to guess the type for each element of a column.

For example, let us say you have a file “weather.tsv” like

      Day	Temp	Wind
	1	96	7
	day2	94	8
	3	65	25
	4	80	10

Where the column “Day” has mixed data types; numbers and string, you will see the above error when you load the file as a data frame using Pandas.

How To Find Data Types of All columns?

We can check data types of all the columns in a data frame with “dtypes”.

df.dtypes

For example, after loading a file as data frame you will see

Day      object
Temp    float64
Wind      int64
dtype: object

How To Change Data Types of a single Column?

There are a few ways to change the datatype of a variable or a column. If you want to change the datatype of just one variable or one column, we can use “astype”. To change the data type the column “Day” to str, we can use “astype” as follows

df.Day = df.Day.astype(str)

You will see the results as

df.dtypes
Day      object
Temp    float64
Wind      int64
dtype: object

How To Change Data Types of One or More Columns?

There is a better way to change the data type using a mapping dictionary.

Let us say you want to change datatypes of multiple columns of your data and also you know ahead of the time which columns you would like to change.

One can easily specify the data types you want while loading the data as Pandas data frame. For example, if you are reading a file and loading as Pandas data frame, you pre-specify datatypes for multiple columns with a
a mapping dictionary with variable/column names as keys and data type you want as values.

Let us use Pandas read_csv to read a file as data frame and specify a mapping function with two column names as keys and their data types you want as values.


df = pd.read_csv("weather.tsv", sep="\t",  
                 dtype={'Day': str,'Wind':int64})
df.dtypes

You can see the new data types of the data frame

Day      object
Temp    float64
Wind      int64
dtype: object

It is also good practice to specify the data types while loading the data frame.

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow to Convert a Column to Datetime type with Pandas Default ThumbnailHow To Select Columns by Data Type in Pandas? Default ThumbnailHow to Combine Year, Month, and Day Columns to single date in Pandas Default ThumbnailHow To Get Data Types of Columns in Pandas Dataframe?

Filed Under: Change data types of columns, Pandas 101, Pandas data type, Python Tips Tagged With: change column type in Pandas, Change data types of columns, Pandas Dataframe

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version