Computation for Linguists

Pandasmonium: Day 3

Dr. Andrew M. Byrd

2025-10-22

Review

  • What did you learn last time?

Recap

  • Converting a variable containing strings to a list?
  • How would we split these words up?
name = "Bob, Susie, Jimbo"
prof = "teacher, doctor, lawyer"
age = "45, 63, 119"

Recap

  • Converting a variable containing strings to a list:
name_split = name.split(",")
prof_split = prof.split(",")
age_split = age.split(",")

Recap

  • How would we convert these lists to a df, making the “name” the column header?

Recap

import pandas as pd

recap_df = pd.DataFrame([prof.split, age.split], columns=name_split)
print(recap_df)

Review Activity

  • Copy the below strings, and convert to lists.
line1 = "English German Spanish French"
line2 = "dog Hund perro chien"
line3 = "cat Katze gato chat"
line4 = "house Haus casa maison"
  • Once you’ve done that, convert to a DataFrame, with the languages in line1 serving as the column headers.

Modifying DataFrames

Load up the languages df

import pandas as pd

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"]
})

df

Adding New Columns

  • We’ve seen that you can combine two dfs together with pd.concat()

  • There’s a simpler way, though, to add a .Series() that should remind you of lists and dicts:

df["is_extinct"] = [True, True, True, False]
df

Dropping Columns & Rows

  • We can drop a .Series() with .drop()
# Drop (returns a new DF unless inplace=True)
df_no_family = df.drop(columns=["family"])
df_no_family

Dropping Columns & Rows

  • Or a row with .drop([index])
# Drop (returns a new DF unless inplace=True) 
df_no_latin = df.drop([0])
df_no_latin

Renaming Columns

  • You can easily rename a column with the .rename() method
df = df.rename(columns={"speakers_millions": "speakers_M"})
df

(Re)setting Indices

  • You can reset an index with .reset_index()
df_reset_no_latin = df_no_latin.reset_index()
df_reset_no_latin

(Re)setting Indices

  • Drop an index with .reset_index(drop=True)
df_ind_drop = df.reset_index(drop=True)
df_ind_drop

(Re)setting Indices

Or convert a .Series() to the index

df_set = df.set_index("language")
df_set

Activity: Change Index of the pd df they built for review activity

  • Load up the df you built for the Review Activity
  • Create three new df by:
    • Dropping the first row, then resetting the index
    • Leave the df intact, but drop the index altogether
    • Convert the Bob column into the index.

Filtering, Sorting, Combining

Sorting

  • You can sort values in all sorts of ways in pd
import pandas as pd

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

df.sort_values(by="location", ascending=True)

Sorting

  • Or if you’d like to organize by the index
df.sort_index() # by index

Combining Series Side-by-Side

  • So far you’ve only combined dfs that are aligned by index
reflexes_verb = pd.Series(
    {"Latin": 8, "Greek": 10, "Sanskrit": 12, "Old English": 6}
)
reflexes_noun = pd.Series(
    {"Latin": 5, "Greek": 7, "Gothic": 4, "Old Church Slavonic": 3}
)

combined = pd.DataFrame({"verb": reflexes_verb, "noun": reflexes_noun})
combined

Handling Missing Data

  • Maybe you’d like to fill in a null value where there’s NaN
    • “Not a Number”
combined_filled = combined.fillna(0)

Handling Missing Data

  • Or perhaps drop those rows all together
combined_dropped = combined.dropna()

Adding Across Rows (fill missing)

  • You can also use .add() to total all of the reflexes in the df by language
total = reflexes_verb.add(reflexes_noun, fill_value=0)
total.sort_values(ascending=False)

Adding Across a Column


{python}
reflexes_verb = pd.Series(
    {"Latin": 8, "Greek": 10, "Sanskrit": 12, "Old English": 6}
)
reflexes_noun = pd.Series(
    {"Latin": 5, "Greek": 7, "Gothic": 4, "Old Church Slavonic": 3}
)
combined = pd.DataFrame({"verb": reflexes_verb, "noun": reflexes_noun})
combined

column_totals = combined.sum(axis=0)
print(column_totals)

Activity: Counting Sounds, Part 1

  • Below is a table of consonants that occur in a constructed language novel.
Chapter s z t k m n
1 35 20 50 42 18 22
2 28 25 40 35 12 20
3 40 18 53 48 17 19
  • Using (multiple!) .add(), tally up how many Cs are in each chapter.

Activity: Counting Sounds, Part 2

Chapter s z t k m n
1 35 20 50 42 18 22
2 28 25 40 35 12 20
3 40 18 53 48 17 19
  • Using .sum() and .add(), identify the total number of fricatives.