
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
Python Pandas - Converting Series to Other Objects
Pandas Series is a one-dimensional array-like object containing data of any type, such as integers, floats, and strings. And the data elements is associated with labels (index). In some situations, you need to convert a Pandas Series into different formats for various use cases like creating lists, NumPy arrays, dictionaries, or even converting the Series into a DataFrame.
In this tutorial, we will learn about various methods available in Pandas to convert a Series into different formats such as lists, NumPy arrays, dictionaries, DataFrames, and strings.
Following are the commonly used methods for converting Series into other formats −
Method | Description |
---|---|
to_list() | Converts the Series into a Python list. |
to_numpy() | Converts the Series into a NumPy array. |
to_dict() | Converts the Series into a dictionary. |
to_frame() | Converts the Series into a DataFrame. |
to_string() | Converts the Series into a string representation for display. |
Converting Series to List
The Series.to_list() method converts a Pandas Series to a Python list, where each element of the Series becomes an element of the returned list. And the type of each element in the list is types as those in the Series.
Example
Here is the example of converting a Pandas Series into a Python list Using the Series.to_list() method.
import pandas as pd # Create a Pandas Series s = pd.Series([1, 2, 3]) # Convert Series to a Python list result = s.to_list() print("Output:",result) print("Output Type:", type(result))
Following is the output of the above code −
Output: [1, 2, 3] Output Type: <class 'list'>
Converting Series to NumPy Array
The Pandas Series.to_numpy() method can be used to convert a Pandas Series into a NumPy array. This method provides a additional features like specifying the data type (dtype), handle missing values (na_value), and control whether the result should be a copy or a view.
Example
This example converts a Series into a NumPy array using the Series.to_numpy() method.
import pandas as pd # Create a Pandas Series s = pd.Series([1, 2, 3]) # Convert Series to a NumPy Array result = s.to_numpy() print("Output:",result) print("Output Type:", type(result))
Output: [1, 2, 3] Output Type: <class 'numpy.ndarray'>
Converting Pandas Series to a Dictionary
The Pandas Series.to_dict() method is used to convert a Series into a Python dictionary, where each label (index) becomes a key and each corresponding value becomes the dictionary's value.
Example
The following example converts a Series into a Python dictionary using the Series.to_dict() method.
import pandas as pd # Create a Pandas Series s = pd.Series([1, 2, 3], index=['a', 'b', 'c']) # Convert Series to a Python dictionary result = s.to_dict() print("Output:",result) print("Output Type:", type(result))
Output: {'a': 1, 'b': 2, 'c': 3} Output Type: <class 'dict'>
Converting a Series to DataFrame
The Series.to_frame() method allows you to convert a Series into a DataFrame. Each Series becomes a single column in the DataFrame. This method provides a name parameter to set the column name of the resulting DataFrame.
Example
This example uses the Series.to_frame() method to convert a Series into a Pandas DataFrame with a single column.
import pandas as pd # Create a Pandas Series s = pd.Series([1, 2, 3], index=['a', 'b', 'c']) # Convert Series to a Pandas DataFrame result = s.to_frame(name='Numbers') print("Output:\n",result) print("Output Type:", type(result))
Output:
Numbers | |
---|---|
a | 1 |
b | 2 |
c | 3 |
Converting Series to Python String
To convert a Pandas Series object to a Python string you can use the he Series.to_string() method, which renders a string representation of the Series.
This method returns a string showing the index and values of the Series. You can customize the output string using various parameters like na_rep (represent missing values), header, index, float_format, length, etc.
Example
This example converts a Series into the Python string representation using the Series.to_string() method.
import pandas as pd # Create a Pandas Series s = pd.Series([1, 2, 3], index=['r1', 'r2', 'r3']) # Convert Series to string representation result = s.to_string() print("Output:",repr(result)) print("Output Type:", type(result))
Output: 'r1 1\nr2 2\nr3 3' Output Type: <class 'str'>