Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Pandas Series.from_csv()

Python | Pandas Series.from_csv()

Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index.

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

For this example we have used a CSV file. To download click here

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))


Output :


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))


Output :

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

RELATED ARTICLES

Most Popular

Recent Comments