![]() |
VOOZH | about |
The tolist() method in Pandas is used to convert a Pandas Series into a Python list. This method returns a new list containing all the elements of the Series in order. Let’s look at an example to understand its usage.
[10, 20, 30, 40] <class 'list'>
This example highlights how tolist() converts a Pandas Series with different data types, including integers, strings, floating-point numbers, and booleans, into a Python list while preserving their types.
[1, 'hello', 3.14, True]
Explanation: The Series contains elements of different data types (integer, string, float, boolean), and tolist() converts them into a list while maintaining their types.
When a Pandas Series contains missing values (NaN), the tolist() method converts them to numpy.nan in the resulting list, ensuring consistency with NumPy's handling of missing data.
[1.0, 2.0, nan, 4.0]
Explanation: The NaN value in the Series is represented as nan in the list output.