# Pandas Lesson 5 Useful Series functions﻿

#### 1) Create a Series to use in this section

``````#import the pandas library
import pandas as pd

#create the Pandas Series include the Data, Name and index
vals = pd.Series([5,8,10,7],name = "Points",
index = ["Henry","John","Jane","Mike"])
#display series
vals``````

#### 2) Sort a Series by value using sort_values()

``````#Starting values
print(vals)

#Default behavoir is ascending
asc_sorted = vals.sort_values(ascending=True)
print(asc_sorted)

#Sort Descending
desc_sorted = vals.sort_values(ascending=False)
print(desc_sorted)

#Sort inplace
#Create a Second Series for this example
val2 = vals
#Sort with inplace = True will change the Series acted on inplace
val2.sort_values(inplace=True)
print(val2)``````

#### 3) Sort a Series by index using sort_index()

``````#Starting values
print(vals)

#Default behavoir is ascending
index_sort_asc = vals.sort_index(ascending=True)
print(index_sort_asc)

#Sort Descending
index_sort_desc = vals.sort_index(ascending=False)
print(index_sort_desc)

#Sort inplace
#Create a Second Series for this example
val3 = vals
#Sort with inplace = True will change the Series acted on inplace
val3.sort_index(inplace=True)
print(val3)``````

#### 3) Add items to a Series using append()

The append method lets you add data to a Series by adding another Series’s data. To use it create a second Series and use the append method to add the data in the new Series to the original Series.

``````#Print Current values
print(vals)

#Add items to a Series using append()
newvals = pd.Series([9,10],name = "Points",
index = ["Peter","Paul"])
vals = vals.append(newvals)
print(vals)``````

#### 4) Replace values in a Series using replace()

The replace method replaces one value in a Series for one that you specify.

``````#Print Current Values
print(vals)

#Replace all 5's with 10's
vals.replace(5,10,inplace=True)
print(vals)
``````

#### 5) Fill null values in a Series using fillna()

``````#Print Current Values
print(vals)

#use replace method to create some null values
# import numpy to use np.nan as null values
import numpy as np
vals.replace(10,np.nan,inplace=True)
print(vals)

#use fillna method to fill the null fields ,you can fill with any value you specify.
vals.fillna(0,inplace=True)
print(vals)``````

#### 6) Remove null values in a Series using dropna()

Drop all null values from a Series using dropna()

``````#Print Current Values
print(vals)

#replace 0 with nan
vals.replace(0,np.nan,inplace=True)
print(vals)

#drop null values
vals.dropna(inplace=True)
print(vals)``````