Python | Pandas Series.astype() to convert Data type of series

Python | Pandas Series.astype() to convert Data type of series

In pandas, a Series is a one-dimensional labeled array that can contain any data type. There are occasions when you might need to change or convert the data type of a Series. The astype() method is used for this purpose.

Let's dive into how to use the Series.astype() method to convert the data type of a series.

Importing Pandas:

First, you'll need to import the pandas library:

import pandas as pd 

Creating a Series:

Here's a basic series:

s = pd.Series([1, 2, 3, 4, 5]) print(s) 

Output:

0 1 1 2 2 3 3 4 4 5 dtype: int64 

Using astype() to Convert Data Type:

  • To convert to float:
s_float = s.astype(float) print(s_float) 

Output:

0 1.0 1 2.0 2 3.0 3 4.0 4 5.0 dtype: float64 
  • To convert to string:
s_str = s.astype(str) print(s_str) 

Output:

0 1 1 2 2 3 3 4 4 5 dtype: object 

Note that the data type becomes object when converting to a string.

  • To convert to a category:

Categories are useful for object data with a limited set of unique values:

s_category = s.astype('category') print(s_category) 

Output:

0 1 1 2 2 3 3 4 4 5 dtype: category Categories (5, int64): [1, 2, 3, 4, 5] 

Important Notes:

  • Always ensure that the conversion makes sense. For instance, if your series contains non-numeric strings, converting to float would raise an error.

  • When dealing with larger DataFrames, converting certain object data types to category can save memory.

  • If you're trying to convert a string representation of a date or time, consider using pd.to_datetime() instead of astype() for more flexibility and accuracy.

The astype() method is versatile and can handle a variety of data type conversions, making it a valuable tool in your pandas toolkit.


More Tags

text-size blocking tmp makecert dll emgucv algorithm negative-lookbehind microsoft-graph-api destructuring

More Programming Guides

Other Guides

More Programming Examples