Pandasmonium: Day 4
2025-10-24
DataFrame()s
.concat(), .drop(), .rename(), .reset_index().sort_index(), .sort_values(ascending=False).fillna(0)df[series_1].add(df[series_2])df.sum().DataFrame(), setting the “Language” Series as the index.import pandas as pd
stops = {
"Language": ["English", "Spanish", "Hindi"],
"Nasals": [3, 3, 4],
"Oral Stops": [6, 6, 5]
}.sum() to find the total number of nasals & oral stops..add() to combine the number of nasals and oral stops in each language.csv and xls filescsv filedf.to_csv("file_name.csv", index=False)
index=True?import pandas as pd
# Create the data
data = {
"Consonant": ["p", "b", "t", "d", "k", "g", "s", "z", "m", "n", "l", "r", "w", "j", "h"],
"Place": ["bilabial", "bilabial", "alveolar", "alveolar", "velar", "velar",
"alveolar", "alveolar", "bilabial", "alveolar", "alveolar", "alveolar",
"labial-velar", "palatal", "glottal"],
"Manner": ["stop", "stop", "stop", "stop", "stop", "stop",
"fricative", "fricative", "nasal", "nasal", "lateral", "trill",
"glide", "glide", "fricative"],
"Voicing": ["voiceless", "voiced", "voiceless", "voiced", "voiceless", "voiced",
"voiceless", "voiced", "voiced", "voiced", "voiced", "voiced",
"voiced", "voiced", "voiceless"]
}
# Create DataFrame
df = pd.DataFrame(data)
# Show DataFrame
print(df)
# Save to CSV
df.to_csv("conlang_c.csv", index=False).csv Activity:df from the Review Activity into a .csv filepip install openpyxl first.import openpyxl
import pandas as pd
data = {
"Consonant": ["p", "b", "t", "d", "k", "g", "s", "z", "m", "n", "l", "r", "w", "j", "h"],
"Place": ["bilabial", "bilabial", "alveolar", "alveolar", "velar", "velar",
"alveolar", "alveolar", "bilabial", "alveolar", "alveolar", "alveolar",
"labial-velar", "palatal", "glottal"],
"Manner": ["stop", "stop", "stop", "stop", "stop", "stop",
"fricative", "fricative", "nasal", "nasal", "lateral", "trill",
"glide", "glide", "fricative"],
"Voicing": ["voiceless", "voiced", "voiceless", "voiced", "voiceless", "voiced",
"voiceless", "voiced", "voiced", "voiced", "voiced", "voiced",
"voiced", "voiced", "voiceless"]
}
# Create DataFrame
df = pd.DataFrame(data)
df.to_excel("conlang_c.xlsx", index=False).xlsx Activity:df from the Review Activity into a .xlsx file.csv and .xlsx.csv and .xlsx files in a pandas dfdf.sum().sum() to add .Series() together by a certain property.groupby().agg aggregates – it collects different types of mathematical operations:
.sum() – total reflexes per family.mean() – average reflexes per familycount – number of languages in that family.import pandas as pd
df = pd.DataFrame({
"Language": ["English","German","Dutch","Spanish","Italian","French","Greek","Hindi","Bengali"],
"Family": ["Germanic","Germanic","Germanic","Romance","Romance","Romance","Hellenic","Indic","Indic"],
"Consonants": [24, 25, 20, 17, 23, 22, 25, 33, 29],
"Vowels": [20, 16, 13, 5, 7, 15, 7, 11, 7]
})Using .agg(), calculate:
.groupby("Family")df to just [["Consonants","Vowels"]]PC:
Mac:
conlang_c.csv and will count the number of stops by place of articulation.matplotlib.pyplot to create a bar chart that shows the number of Cs by place of articulation.
pddf["col"], df[["col1","col2"]]df.iloc[0], df.iloc[1:3]df.loc["a"], df.loc["a":"c"]df.loc["a","col"] or df.iloc[0, 1]df[df["col"] == value], df[(cond1) & (cond2)]s1.add(s2, fill_value=0)
pd.concat([df1, df2], axis=0, ignore_index=True) # vertical
pd.concat([s1, s2], axis=1) # side-by-side