Computation for Linguists

Pandasmonium, Day 2

Dr. Andrew M. Byrd

2025-10-20

Review

  • What did you learn last time?

Recap from Last Time

  • pd.Series(), pd.DataFrame
  • Creating df
    • pd.DataFrame({ column_name : [value_1, value_2] })
    • pd.DataFrame(dict)
    • pd.concat([series_1, series_2], axis=1)
    • pd.DataFrame.from_dict(dict, orient="index", columns=[dict_value])
    • pd.DataFrame(list(dict.items()), columns=[key, value])

Review Activity: PIE time

  • Using pd.DataFrame({ ... }), create the following table
Root Meaning Latin Greek English
*bʰer- ‘to carry’ ferō pʰérō bear
*ĝenh₁- ‘to beget’ gignō gígnomai kin
*ped- ‘foot’ pēs poús foot
*doh₃- ‘to give’ dídōmi donate

Indexing and Selecting

Load up our previous pd

import pandas as pd

fresh_df = pd.DataFrame({
    "language": ["Latin", "Greek", "Sanskrit", "Finnish"],
    "family":   ["Indo-European", "Indo-European", "Indo-European", "Uralic"],
    "speakers_millions": [0, 0, 0, 6],
    "location": ["Italy", "Greece", "India", "Finland"]
})

print(fresh_df)

Inspecting a DataFrame

  • Run this code - what do .head(), .tail(), .shape, .columns, and .index do?
df.head(2) 
df.tail(2) 
df.shape
df.columns
df.index
df.info()

Accessing Single Columns

  • We can access a single .Series() using df[ ]
df["language"]           # Series
  • Does this remind you of anything?

Accessing Multiple Columns

  • Or multiple columns using df[[ ]]
df[["language","family"]] # DataFrame

Selecting Rows by Integer Position

  • We can access specific rows using df.iloc
    • “integer location”
df.iloc[0]               # first row
df.iloc[1:3]             # slice rows 1..2

Selecting Rows By Label

  • Or rows with a custom index using df.loc
# Rows by label (after we set a custom index)
df2 = df.copy()
df2.index = ["a","b","c","d"]
df2.loc["b"]             # row labeled 'b'

Cell Access (Row + Column)

  • We can access a specific cell by label/column
df.loc[0, "language"]        # cell by label/column
  • Or using .iloc with specific coordinates
df.iloc[0, 0]                # cell by position
  • We can also slice in 2D ways:
df.iloc[0:3, 0:2]            # rows 0..2, cols 0..1

Boolean Filtering

  • And lastly we can filter our table using Boolean operators for specific properties:
ie = df[df["family"] == "Indo-European"]
ie

Complex Filtering

  • Use the operators & (and) and | (or)
df[(df["family"]=="Indo-European") & (df["location"]=="India")]
df[(df["family"]=="Indo-European") | (df["location"]=="Finland")]

Complex Filtering

  • Place ~ in front of the filter to mean not
df[~(df["family"]=="Indo-European")]

Complex Filtering

  • To sort a df alphabetically by a specific .Series(), use the method .sort_values()
df.sort_values(["location"])

Counting

  • You can add up the total number of a .Series() using the .sum() method
df["speakers_millions"].sum()
  • Which you can display as a regular int using int():
int(df["speakers_millions"].sum())

Activity: English Vowels

  • Copy the below code, and complete the values for “height”, “backness”, “tense”, and “roundedness”
import pandas as pd

eng_vowels = pd.DataFrame({
    "symbol": ["i", "ɪ", "e", "ɛ", "æ", "u", "ʊ", "o", "ɔ", "ɑ", "ʌ", "ə"],
    "height": [], #choose from "high", "mid", and "low"
    "backness": [], # choose from "front", "back", and "central"
    "tense":   [], #choose from True or False
    "rounded": [], #choose from True or False
})

eng_vowels

Activity: English Vowels

  1. Filter the df: show only high back rounded vowels

  2. Negation and slicing: display all vowels that are not tense.

  3. Then display only the first three vowels of that group.

  4. Sort the df by height and backness.

  5. Count how many vowels are rounded, tense and both.