Pandas Lesson 2 Create a Series

  1. Series Constructor
  2. Create an Empty Series
  3. Create a Series from a Python Dictionary
  4. Create a Series from a List
  5. Create a Series from an Numpy Array
  6. Create a Series from a CSV
  7. Create a Series from JSON

1) Series Constructor

The Series Constructor is as follows:

Series(data=Noneindex=Nonedtype=Nonename=Nonecopy=Falsefastpath=False)

  • data: Array-like, iterable, dict or scalar value
  • index: Array-like or index
  • dtype (optional): string, numpy dtype or ExtensionDtype, will be inferred if not provided.
  • name (optional): Name of the Series
  • copy: copy input data (False by default)
  • fastpath: Internal parameter should not be changed

2) Create an Empty Series

To create an empty Series import the pandas library and use the Series() constructor. The name parameter is optional.


import pandas as pd
#name is optional
first_series = pd.Series(name = "An Empty Series")
print(first_series)
Notebook Output

3) Create a Series from a Python Dictionary

Pass the dictionary as the data parameter of the Series.


import pandas as pd
#create a dict
dict_temps= {"Houston":99,"Dallas":100,"Austin":98}
temperatures = pd.Series(dict_temps,name = "Texas Temps")
print(temperatures)
Notebook Output

4) Create a Series from a List

To create a Series from a List you must create a list with data and pass it to the data parameter of the Series constructor. You also have the option of defining the index. You can use another list to define the index. If the index is not provided then it will create numeric indexes.


import pandas as pd
#create a list for data and a list for index (if index is not provided it will use autonumber)
temps= [99,100,98]
cities = ["Houston","Dallas","Austin"]
temperatures = pd.Series(temps,index = cities ,name = "Texas Temps")
print(temperatures)
Notebook Output

5) Create a Series from an Numpy Array

To create a Series from a Numpy Array. Import Numpy and create the array and pass it to the data parameter of the Series constructor. You also have the option of defining the index. You can use a list to define the index. If the index is not provided then it will create numeric indexes.


import pandas as pd
import numpy as np
#create a numpy array for data and a list for index (if index is not provided it will use autonumber)
np_array = np.array([99,100,98])
cities = ["Houston","Dallas","Austin"]
temperatures = pd.Series(np_array,index = cities ,name = "Texas Temps")
print(temperatures)
Notebook Output

6) Create a Series from a CSV

You can use the read_csv() method to create a Pandas Series from a csv file by using the squeeze parameter. If the csv has only one column it will create a Series instead of a DataFrame (default). In the example below I am using a csv I uploaded to github.

Example csv:

Temps
99
100
98

import pandas as pd
#using an online 1 column csv file
csvurl = "https://raw.githubusercontent.com/buildandteach/pandasbook/master/series_temperatures.csv"
temperatures = pd.read_csv(csvurl,squeeze = True)
cities = ["Houston","Dallas","Austin"]
temperatures.index = cities
print(temperatures)
print (isinstance(temperatures, pd.Series))#Confirm that the object is a Series
Notebook Output

7) Create a Series from JSON

To create a Series from a json file. Use the read_json() method. By Default the read_json method creates a DataFrame but you can use the squeeze() method to create a Series from a DataFrame.

Example json:

{"Temps":{"Houston":99,"Dallas":100,"Austin":98}}

import pandas as pd
jsonurl = "https://raw.githubusercontent.com/buildandteach/pandasbook/master/data.json"
tempsjson = pd.read_json(jsonurl)
#Convert dataframe to Series
seriesjson = tempsjson.squeeze()
print(seriesjson)
print (isinstance(seriesjson , pd.Series))
#Confirm that the object is a Series
Notebook Output