Dataframe
Study guide: DISC-DA
A Dataframe (data frame) is a rectangular table that holds a data set in memory for analysis, with variables in columns and observations in rows. Every value in a column has the same type, while different columns can have different types. It is the central structure of R (data.frame, and the tidyverse's tibble) and of Python's pandas library (DataFrame), which describes it as a two-dimensional labelled structure that can be thought of like a spreadsheet or an SQL table.
A dataframe has n rows, the observations such as policies or claims, and p columns, the variables. Most dataframe tools assume the tidy layout, each variable in its own column, each observation in its own row and each value in its own cell. The type of each column (integer, double, character, date-time) decides which operations make sense on it.
The operations on a dataframe act on its rows, its columns or groups of rows, and each has a counterpart in SQL:
| Task | R (dplyr) | pandas | SQL |
|---|---|---|---|
| Keep rows meeting a condition | filter() |
boolean indexing | WHERE |
| Sort rows | arrange(), desc() |
sort_values() |
ORDER BY |
| Remove duplicate rows | distinct() |
drop_duplicates() |
DISTINCT |
| Choose columns | select() |
a list of column names | SELECT a, b |
| Add a derived column | mutate() |
assign() |
SELECT expr AS name |
| Group and summarize | group_by(), summarize() |
groupby(), agg() |
GROUP BY |
| Combine tables on a key | left_join() and others |
merge() |
JOIN |
Filtering changes which rows are present but not their order. Sorting changes the order of the rows and never their number, and sorting on several columns uses each later column to break ties in the earlier ones. Neither touches the columns. Selecting and adding columns leave the rows alone.
dplyr's functions never modify their input, and most pandas operations return a copy. A filtered or summarized result must be assigned to a name, either a new object or the original one, or it is printed and lost.
What are this policy dataframe's dimensions after each step of the analysis? A policy dataframe has 10,000 rows and 12 columns. An analyst keeps the 2,400 rows written through the agency channel, selects 4 columns (territory, exposure, premium and losses), adds a loss ratio column, then groups by territory (8 territories) and summarizes total exposure, total premium and total losses.
Answer
- Filtering changes only the rows, selecting and adding columns change only the columns, and a grouped summary returns one row per group with the grouping column plus one column per summary.
Step Rows Columns Start 10,000 12 Filter to agency 2,400 12 Select 4 columns 2,400 4 Add loss ratio 2,400 5 Group and summarize 8 4
- The policy-level loss ratio column is not carried into the summary. A territory's loss ratio is its total losses over its total premium, computed after summarizing, not an average of the policies' ratios.
Grouped summaries
In a grouped summary, the rows are split by the value g_i of a grouping variable, and a summary function h (a count, sum or mean) is applied to a variable j within each group. The summary for group g is
s_g = h\big(\{\, x_{ij} : g_i = g \,\}\big)
x_{ij}is the value of variablejfor observationi.
The result has one row per group, with the grouping column plus one column per summary.
In R, a missing value (NA) makes mean() and sum() return NA unless na.rm = TRUE drops it. n() and size() count rows in a group, including those with a missing value, while count() on a column and a mean with missing values removed use only the non-missing ones. In pandas, size() counts the rows of each group and count() counts only the non-null values in each column, the same split as COUNT(*) against COUNT(column) in SQL (Missing Data).
Including the number of rows in each group with a summary shows when a group's average rests on very little data, which otherwise can put the smallest groups at the top of a ranking.
What does this pipeline return from the claims dataframe, and what would change without
na.rm = TRUE? A claims dataframeclaimshas seven rows.
claim terr ay paid C1 T1 2024 4,000 C2 T1 2024 6,000 C3 T2 2024 2,500 C4 T2 2023 9,000 C5 T2 2024 NA C6 T3 2024 12,000 C7 T2 2024 3,500 An analyst runs:
claims |> filter(ay == 2024) |> group_by(terr) |> summarize(n = n(), avg_paid = mean(paid, na.rm = TRUE)) |> arrange(desc(avg_paid))Answer
- The filter drops C4, an accident year 2023 claim, leaving six rows.
- Grouping by territory and summarizing gives one row per territory, and
n()counts rows, including C5.- The averages are
(4{,}000 + 6{,}000)/2 = 5{,}000for T1,(2{,}500 + 3{,}500)/2 = 3{,}000for T2 and12{,}000for T3.- Sorted by descending average, the result is T3 (
n = 1, 12,000), T1 (n = 2, 5,000) and T2 (n = 3, 3,000).- T2's count is 3 but its average uses only the 2 claims with a paid amount. Without
na.rm = TRUE, T2's average would beNA.- The count column also shows that T3 tops the list on a single claim.
Data usually reach a dataframe from a database query or a file, are combined with a join, checked by validation, and prepared for plotting in Exploratory Data Analysis or for a model (Data Preparation, Feature Engineering).
Swapping filter and select Filtering keeps rows that meet a condition, and selecting keeps columns by name. An answer that "filters" a column out, or "selects" policies over $1,000, has them swapped.