3 Data Types and Data Structures
Learn how variable meaning, measurement scales, and table structure determine which data summaries and comparisons are appropriate.
meaning and representation
A is a recorded characteristic that can vary across observations. It can be described by its meaning—categorical or quantitative—and by its computer representation, such as an integer, floating-point number, string, or Boolean. These descriptions are complementary, not interchangeable.
A postal code may be stored as an integer but function as a label, so adding or averaging postal codes has no useful meaning. A rating from to may also be stored as an integer even though the gaps between rating levels may not represent equal differences. Numeric coding alone does not make a category a measurement.
Quantitative variables may be discrete, with countable values such as number of visits, or continuous, with measured values that can vary along a continuum, such as height. Categorical variables group observations into labels, such as region or device type. A binary has two possible categories, often represented as True/False or 0/1.
Programming-language data types determine which values can be represented and which operations are available. Common types include integers, decimal numbers, strings, and Booleans; lists, tuples, and dictionaries can hold multiple values. For example, the Python string "02139" preserves a leading zero, whereas the integer 2139 does not.
Measurement scales and valid comparisons
A describes what comparisons a ’s values support and helps prevent misleading calculations.
: Categories differ but have no inherent order. Blood type and city are examples. Counts, proportions, and the most common category are useful summaries.
: Categories have an order, but equal spacing between ranks is not established. Satisfaction levels such as low, medium, and high are an example. Counts, proportions, medians, and rank comparisons are useful.
: Values are ordered and have equal intervals, but zero does not represent an absolute absence. Temperature in degrees Celsius and calendar year are examples. Differences, means, and standard deviations are useful.
: Values have equal intervals and a meaningful zero, so ratios are meaningful. Mass, duration, and number of visits are examples. Differences, means, and ratios such as “twice as much” are meaningful.
For example, is not twice as warm as , because the Celsius scale has an arbitrary zero. On the Kelvin scale, is twice . A satisfaction score may likewise be ordered without the difference between and necessarily matching the difference between and .
Scales guide analysis but do not replace understanding how a was measured. A numeric identifier remains a category, and converting labels into numbers does not by itself justify arithmetic on them.
Tables and observational units
A organizes data into rows and columns. In a common tidy arrangement, each column represents a , each row represents one observation, and each represents one kind of observational unit.
Consider data with columns student_id, course, and score. A row such as student 101, Biology, score 84 records one student’s result in one course. The observation is a student-course result, not simply a student. The student_id links multiple rows belonging to the same student.
A software structure such as a pandas DataFrame has labeled rows and columns and can contain columns with different data types. This is useful for real-world data in which, for example, one column contains text labels and another contains numbers.
Reshaping data without losing meaning
Identify the —what one row represents—before summarizing a . In the student-course example, counting rows gives the number of course results, not the number of unique students. To count students, count distinct IDs.
The same information can be arranged in different shapes. In , repeated measurements appear on separate rows; in , they appear in separate columns. For student 101, the long arrangement has separate Biology and History rows with scores 84 and 91. The wide arrangement has one row for the student, with separate Biology and History columns containing those scores.
is often convenient for grouping, plotting, or analyzing repeated measurements by a category. can make side-by-side comparisons convenient. Reshaping does not change what the data mean, but an incorrect reshape—or a missing identifier during a join—can duplicate, misalign, or lose observations. Keep the and identifiers clear when transforming data.