HomeLanguagesPython | Pandas Series.to_dict()

Python | Pandas Series.to_dict()

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.to_dict() function is used to convert the given Series object to {label -> value} dict or dict-like object.

Syntax: Series.to_dict(into=)

Parameter :
into : The collections.Mapping subclass to use as the return object.

Returns : value_dict : collections.Mapping

Example #1: Use Series.to_dict() function to convert the given series object to a dictionary.




# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series(['New York', 'Chicago', 'Toronto', 'Lisbon', 'Rio', 'Moscow'])
  
# Create the Datetime Index
didx = pd.DatetimeIndex(start ='2014-08-01 10:00', freq ='W', 
                     periods = 6, tz = 'Europe/Berlin') 
  
# set the index
sr.index = didx
  
# Print the series
print(sr)


Output :

Now we will use Series.to_dict() function to convert the given series object to a dictionary.




# convert to dictionary
sr.to_dict()


Output :


As we can see in the output, the Series.to_dict() function has successfully converted the given series object to a dictionary.
 
Example #2: Use Series.to_dict() function to convert the given series object to a dictionary.




# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series([19.5, 16.8, 22.78, 20.124, 18.1002])
  
# Print the series
print(sr)


Output :

Now we will use Series.to_dict() function to convert the given series object to a dictionary.




# convert to dictionary
sr.to_dict()


Output :

As we can see in the output, the Series.to_dict() function has successfully converted the given series object to a dictionary.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS