Python | Панды Series.from_csv ()

Опубликовано: 27 Марта, 2022

Серия Pandas - это одномерный массив ndarray с метками осей. Этикетки не обязательно должны быть уникальными, но должны быть хешируемого типа. Объект поддерживает индексирование как на основе целых чисел, так и на основе меток и предоставляет множество методов для выполнения операций, связанных с индексом.

Pandas Series.from_csv() function is used to read a csv file into a series. It is preferable to use the more powerful pandas.read_csv() for most general purposes.

Syntax: Series.from_csv(path, sep=’, ‘, parse_dates=True, header=None, index_col=0, encoding=None, infer_datetime_format=False)

Parameter :
path : string file path or file handle / StringIO
sep : Field delimiter
parse_dates : Parse dates. Different default from read_table
header : Row to use as header (skip prior rows)
index_col : Column to use for index
encoding : a string representing the encoding to use if the contents are non-ascii
infer_datetime_format : If True and parse_dates is True for a column, try to infer the datetime format based on the first datetime string

Returns : Series

В этом примере мы использовали файл CSV. Для скачивания нажмите здесь

Example #1: Use Series.from_csv() function to read the data from the given CSV file into a pandas series.

# importing pandas as pd
import pandas as pd
  
# Read the data into a series
sr = pd.Series.from_csv("nba.csv")
  
# Print the first 10 rows of series
print(sr.head(10))

Выход :


As we can see in the output, the Series.from_csv() function has successfully read the csv file into a pandas series.
 
Example #2 : Use Series.from_csv() function to read the data from the given CSV file into a pandas series. Use the 1st column as an index of the series object.

# importing pandas as pd
import pandas as pd
  
# Read the data into a series
sr = pd.Series.from_csv("nba.csv", index_col = 1)
  
# Print the first 10 rows of series
print(sr.head(10))

Выход :

As we can see in the output, the Series.from_csv() function has successfully read the csv file into a pandas series.

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

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