Select the rows of a pd.Series that satisfy a condition

Strings that satisfy a condition

You can give a Series or list of bools to loc, but you can’t give them to iloc.

"Create a smaller series that contains just the states with a 'k'."

import sys
import pandas as pd

states = {
    "AL": "Alabama",
    "AK": "Alaska",
    "AZ": "Arizona",
    "AR": "Arkansas",
    "CA": "California"
}

series = pd.Series(data = states, name = "state")
series.index.name = "abbr"
print(series)
print()

#Create a Series whose values are 5 bools.
seriesOfBools = series.str.contains("k")
print(seriesOfBools)
print()

#Create a Series whose values are 2 strings.
#A Series of bools (or a list of bools) used for the following purpose is called a mask.
shorterSeries = series[seriesOfBools]   #or shorterSeries = series.loc[seriesOfBools]
                                        #or shorterSeries = series[series.str.contains("k")]
print(shorterSeries)
print()

#You can do the same thing with a list of bools instead of a Series of bools.
listOfBools = [False, True, False, True, False] #or listOfBools = ["k" in state for state in series]
shorterSeries = series[listOfBools]             #or shorterSeries = series.loc[listOfBools]
print(shorterSeries)

sys.exit(0)
abbr
AL       Alabama
AK        Alaska
AZ       Arizona
AR      Arkansas
CA    California
Name: state, dtype: object

abbr
AL    False
AK     True
AZ    False
AR     True
CA    False
Name: state, dtype: bool

abbr
AK      Alaska
AR    Arkansas
Name: state, dtype: object

abbr
AK      Alaska
AR    Arkansas
Name: state, dtype: object

Try each of the following conditions. See Working with text data and the Method summary.

seriesOfBools = series.str.contains("k")
seriesOfBools = series.str.endswith("a")
seriesOfBools = series.str.startswith("Ar")
seriesOfBools = series == "Alaska"   #is equal to
seriesOfBools = series.str.len() == 7

seriesOfBools = series.index.str.startswith("A")
seriesOfBools = series.index.str.startswith("A") & series.str.endswith("a") #and
seriesOfBools = series.index.str.startswith("A") | series.str.endswith("a") #or
seriesOfBools = ~series.index.str.startswith("A")                           #not

Numbers that satisfy a condition

"Create a smaller series containing only some of the rows of the original Series."

import sys
import numpy as np
import pandas as pd

data = np.arange(10.0, 60.0, 10)
series = pd.Series(data = data, name = "temperature")
series.index.name = "day"
print(series)
print()

bigValues = series[series >= 30.0]                    #series >= 30.0 is a Series of 5 bools.  3 of them are True.
bigValues.name = "bigValues"
print(bigValues)
print()

smallIndices = series[series.index <= 2]              #series.index <= 2 is a Series of 5 bools.  3 of them are True.
smallIndices.name = "smallIndices"
print(smallIndices)
print()

both = series[(series >= 30.0) & (series.index <= 2)] #(series >= 30.0) & (series.index <= 2) is a Series of 5 bools.  1 of them is True.
both.name = "both"
print(both)

sys.exit(0)
day
0    10.0
1    20.0
2    30.0
3    40.0
4    50.0
Name: temperature, dtype: float64

day
2    30.0
3    40.0
4    50.0
Name: bigValues, dtype: float64

day
0    10.0
1    20.0
2    30.0
Name: smallIndices, dtype: float64

day
2    30.0
Name: both, dtype: float64