Как преобразовать серию Pandas в список Python?
In this article, we will discuss how to convert a Pandas series to a Python List and it’s type. This can be done using the tolist() method.
Example 1:
Python3
import pandas as pd evenNumbers = [2, 4, 6, 8, 10] evenNumbersDs = pd.Series(evenNumbers)print("Pandas Series and type")print(evenNumbersDs)print(type(evenNumbersDs)) print("
Convert Pandas Series to Python list")print(evenNumbersDs.tolist())print(type(evenNumbersDs.tolist())) |
Выход:

В приведенном выше примере мы передаем данные серии Panda как четные числа. Поскольку индекс не указан, по умолчанию 0 взятый
Пример 2:
Python3
import pandas as pdimport numpy as np data = np.array(["ant","bat","cat","dog"])series = pd.Series(data,index=[100,101,102,103])print(series)print(type(series)) print("
Convert Pandas Series to Python list")print(series.tolist())print(type(series.tolist())) |
Выход:

В приведенном выше примере мы передаем данные серии Panda как np.array (['ant', 'bat', 'cat', 'dog']).
Индексируйте как [100,101,102,103]
Example 3:
Python3
# Series from dictionaryimport pandas as pdimport numpy as np marks = {"Maths" : 100., "Physics" : 90., "Chemistry" : 85.}series = pd.Series(marks) print(series)print(type(series)) print("
Convert Pandas Series to Python list")print(series.tolist())print(type(series.tolist())) |
Выход:

Example 4:
Python3
# Get First 3 student marks and# convert as listimport pandas as pdseries = pd.Series([100, 90, 80, 90, 85], index=["Student1", "Student2", "Student3", "Student4", "Student5"]) # retrieve the first three elementprint(series[:3])print(series[:3].tolist())print(type(series[:3].tolist())) |
Выход:

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.