Computation for Linguists

Pandasmonium, Day 1

Dr. Andrew M. Byrd

2025-10-17

Review

  • What did you learn last time?

Recap from Last Time

  • Conditionals: if = 1 x
  • Loops: for = multiple times
  • Cleaning text with .strip(), .lower()

Review Activity

  1. Copy the code below.
  2. Use a for loop and a conditional to sort each letter of the word “computation” into consonants & vowels.
  3. If the character is a vowel, print “x is a vowel.” If it is not a vowel, print “x is not a vowel.”
word = "computation"
vowels = "aeiou"

Setting up Pandas

Installing Pandas

  • PC:
pip install pandas
  • Mac:
pip3 install pandas

Loading Pandas

  • To run in Python:
import pandas as pd

⚠️ Avoid file-name shadowing: Don’t name your scripts pandas.py, re.py, random.py, etc.

How does Pandas Work?

What is Pandas?

  • Pandas, short for Panel Data, is a Python library
    • libraries are like LaTeX packages
  • Python programmers often use pandas to sort, create, and analyze tabular data
    • This will allow us to compute data found in spreadsheets (such as lexica and corpora).

Panda Power

  • Note: pandas is built on top of a NumPy array.
    • This package allows you to compute high-level mathematical functions
    • For this reason, you can compute these things within pandas as well! (but not just yet)

Pandas Series

  • At the heart of a pd df (pandas dataframe), are .Series():
    • 1-D labeled data
    • a single labeled column or row
import pandas as pd

langs = pd.Series(["Latin", "Greek", "Sanskrit"], name="language")
langs
  • Right now, name="language" isn’t doing anything, but will later on.

Pandas Series

  • You can give specific labels to the index of the .Series()
import pandas as pd

langs = pd.Series(["Latin", "Greek", "Sanskrit", "Finnish"], index=["a","b","c","d"], name="language")
langs

Pandas Series

  • pd.Series():
import pandas as pd

langs = pd.Series(["Latin", "Greek", "Sanskrit", "Finnish"], index=["a","b","c","d"], name="language")
langs
  • Are conceptually similar to dict:
langs = {"a": "Latin", "b": "Greek", "c":"Sanskrit", "d":"Finnish"}

Pandas DataFrames

  • Pandas .DataFrame() is the most popular way to create csv structure in Python
import pandas as pd

lang_df = pd.DataFrame(langs)

Pandas DataFrames

  • You can combine Series that share the same index to create a DataFrame
  • To do so let’s create a second series.
    • Note the indices!
import pandas as pd

fam = pd.Series(["Indo-European", "Indo-European", "Indo-European", "Uralic"], index=["a","b","c","d"], name="family")
fam

Pandas DataFrames

  • And a third:
import pandas as pd
speakers = pd.Series([0, 0, 0, 6], index=["a","b","c","d"], name="speakers_millions")
speakers

Pandas DataFrames

  • We can combine them all together into a df
import pandas as pd

langs = pd.Series(["Latin", "Greek", "Sanskrit", "Finnish"], index=["a","b","c","d"], name="language")
fam = pd.Series(["Indo-European", "Indo-European", "Indo-European", "Uralic"], index=["a","b","c","d"], name="family")
speakers = pd.Series([0, 0, 0, 6], index=["a","b","c","d"], name="speakers_millions")

languages_df = pd.concat([langs, fam, speakers], axis=1)
print(languages_df)
  • What does axis=1 do?

Pandas DataFrames

  • New method: pd.concat()
    • concatenate pd object with other(s) to make a df.
  • Try this:
import pandas as pd

location = pd.Series(["Italy", "Greece", "India", "Finland"], index=["a","b","c","d"], name="location")
new_df = pd.concat([languages_df, location], axis=1)

print(new_df)

Pandas DataFrames

  • Or you can just bypass the middle step and create the df initially:
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"]
}, index=["a","b","c","d"])

print(fresh_df)

Pandas Activity: Phonemic Inventory

Using Pandas, recreate the following table:

sound voicing place manner
0 /p/ voiceless bilabial stop
1 /b/ voiced bilabial stop
2 /t/ voiceless alveolar stop
3 /d/ voiced alveolar stop
4 /s/ voiceless alveolar fricative
5 /h/ voiceless glottal fricative

Going from []/{} to df

Converting Lists to a Series

  • If you think about it, a list is just a single column of a .DataFrame()
  • And for this reason, it’s super easy to convert it to a .Series()
import pandas as pd

gettysburg = ["Four", "score", "and", "seven", "years", "ago"]
getty = pd.Series(gettysburg)

print(getty)

Converting a dict to .Series()

  • How would we convert a dict to a .DataFrame()?
languages = {
    "French": "Romance",
    "Spanish": "Romance",
    "Italian": "Romance",
    "English": "Germanic"
}
  • This really depends what you want your table to look like!

Converting a dict to df

  • You can easily convert using the below code:
import pandas as pd

languages = {
    "French": ["Romance"],
    "Spanish": ["Romance"],
    "Italian": ["Romance"],
    "English": ["Germanic"]
}
lang_df = pd.DataFrame(languages)
print(lang_df)
  • But:
    • Values must be placed inside of a list for this to work
    • Keys -> column labels
    • Values -> column values

Converting a dict to df

  • It works quite well, though, with multi-item lists set as the value
import pandas as pd

languages = {
    "French": ["Romance", 1, "b"],
    "Spanish": ["Romance", 1, "c"],
    "Italian": ["Romance", 4, "a"],
    "English": ["Germanic", 10, "x"]
}
lang_df = pd.DataFrame(languages)
print(lang_df)

Making the Key the Index

  • Let’s go back to single values within a dict
  • We can use .from_dict(dict, orient="index", columns = [value]) to make the key the index
import pandas as pd

languages = {
    "French": "Romance",
    "Spanish": "Romance",
    "Italian": "Romance",
    "English": "Germanic"
}

lang_df = pd.DataFrame.from_dict(languages, orient="index", columns=["Family"])
print(lang_df)

Keys & Values as Separate Columns

  • Or we can use .DataFrame(list(dict.items()), columns = [key, value]) to set the key as one column, and the value as the second
import pandas as pd

languages = {
    "French": "Romance",
    "Spanish": "Romance",
    "Italian": "Romance",
    "English": "Germanic"
}

lang_df = pd.DataFrame(list(languages.items()), columns=["Language", "Family"])
print(lang_df)

DF Activity

  • Build upon the most recent df, where the language & family were in separate columns.
  • Create a new df called new_info, where you add the following values you will need to look up:
    • Primary (original?) location
    • Number of speakers (in millions)
    • Writing System
    • ISO code
  • Concatenate this list, saving as lang_updated