Data Types and Structures

Difficulty: Beginner

Understanding data types is one of the most fundamental skills in data analytics. Every dataset you encounter will contain columns or fields with different types of data, and correctly identifying these types determines which analytical techniques, visualizations, and statistical methods you can apply. Treating a categorical variable as numeric, or vice versa, can lead to completely meaningless results.

Numeric data represents quantities and can be further divided into two subcategories. Continuous numeric data can take any value within a range, such as temperature (36.5 degrees), height (175.2 cm), or revenue ($45,230.50). Discrete numeric data can only take specific integer values, such as the number of customers (42), number of defects (3), or number of orders (156). The distinction matters because continuous data is typically analyzed with different statistical methods than discrete data.

Categorical data represents groups or categories without any inherent numerical value or order. Examples include color (red, blue, green), country (India, USA, Japan), or product category (electronics, clothing, food). You cannot meaningfully average or sum categorical values. Instead, you analyze them through frequency counts, proportions, and mode. A special subtype is binary data, which has exactly two categories, such as yes/no, true/false, or male/female.

Ordinal data is a hybrid that has categories with a meaningful order, but the distances between categories are not necessarily equal. Education level (high school, bachelor's, master's, PhD), customer satisfaction (very dissatisfied, dissatisfied, neutral, satisfied, very satisfied), and T-shirt sizes (S, M, L, XL) are all ordinal. You can rank ordinal data and compute median, but calculating a mean is generally not meaningful because the intervals are not uniform.

The distinction between structured and unstructured data is equally important. Structured data fits neatly into tables with rows and columns, like spreadsheets and relational databases. Unstructured data includes text documents, images, audio, video, and social media posts that do not have a predefined schema. Semi-structured data like JSON and XML falls in between, having some organizational properties but not fitting into rigid tables. In modern analytics, roughly 80% of data generated is unstructured, making text processing and natural language processing increasingly important skills.

Code examples

Identifying Data Types

# Sample dataset as a list of dictionaries
employees = [
    {"name": "Alice", "age": 28, "department": "Engineering", "salary": 75000.50, "level": "Junior"},
    {"name": "Bob", "age": 35, "department": "Marketing", "salary": 82000.00, "level": "Senior"},
    {"name": "Charlie", "age": 42, "department": "Engineering", "salary": 95000.75, "level": "Lead"},
    {"name": "Diana", "age": 31, "department": "Sales", "salary": 68000.25, "level": "Mid"},
]

# Classify each field
data_types = {
    "name": "Text (Categorical - Nominal)",
    "age": "Numeric (Discrete)",
    "department": "Categorical (Nominal)",
    "salary": "Numeric (Continuous)",
    "level": "Categorical (Ordinal)",
}

for field, dtype in data_types.items():
    print(field + ": " + dtype)

Each field in a dataset has a specific data type that determines how it should be analyzed. Name is text/nominal (no order), age is discrete numeric, department is nominal categorical, salary is continuous numeric, and level is ordinal (Junior < Mid < Senior < Lead).

Working with Categorical Data

# Count frequencies of categorical data
orders = [
    {"product": "Laptop", "region": "North"},
    {"product": "Phone", "region": "South"},
    {"product": "Laptop", "region": "North"},
    {"product": "Tablet", "region": "East"},
    {"product": "Phone", "region": "North"},
    {"product": "Phone", "region": "South"},
    {"product": "Laptop", "region": "East"},
    {"product": "Tablet", "region": "South"},
]

# Frequency count for products
product_counts = {}
for order in orders:
    product = order["product"]
    product_counts[product] = product_counts.get(product, 0) + 1

print("Product Frequencies:")
for product, count in sorted(product_counts.items()):
    print("  " + product + ": " + str(count))

# Find the mode (most common product)
mode = max(product_counts, key=product_counts.get)
print("Most Popular: " + mode)

For categorical data, frequency counting is the primary analysis technique. We use a dictionary to count occurrences and find the mode (most frequent value). Note that when two items have the same count, max() returns the first one encountered.

Ordinal Data Encoding

# Ordinal data has a meaningful order
education_order = {
    "High School": 1,
    "Bachelor's": 2,
    "Master's": 3,
    "PhD": 4,
}

applicants = [
    {"name": "Alice", "education": "Master's"},
    {"name": "Bob", "education": "PhD"},
    {"name": "Charlie", "education": "Bachelor's"},
    {"name": "Diana", "education": "High School"},
    {"name": "Eve", "education": "Master's"},
]

# Sort applicants by education level
sorted_applicants = sorted(applicants, key=lambda x: education_order[x["education"]])

print("Applicants by Education Level:")
for a in sorted_applicants:
    rank = education_order[a["education"]]
    print("  " + a["name"] + " - " + a["education"] + " (rank " + str(rank) + ")")

Ordinal data can be encoded as integers to enable sorting and comparison. The mapping preserves the natural order. This technique is called ordinal encoding and is commonly used in data preprocessing for machine learning.

Structured vs Unstructured Data

# Structured data: fits in a table
structured_data = [
    {"id": 1, "city": "Mumbai", "temp": 32.5},
    {"id": 2, "city": "Delhi", "temp": 38.1},
    {"id": 3, "city": "Bangalore", "temp": 27.8},
]

# Unstructured data: free-form text
reviews = [
    "Great product! Loved the battery life.",
    "Terrible experience. Returned it immediately.",
    "Average quality for the price. Nothing special.",
]

# Extracting structure from unstructured data
positive_words = ["great", "loved", "excellent", "amazing"]
negative_words = ["terrible", "bad", "worst", "returned"]

print("Sentiment Analysis (basic):")
for review in reviews:
    words = review.lower().split()
    pos = sum(1 for w in words if w.strip(".,!") in positive_words)
    neg = sum(1 for w in words if w.strip(".,!") in negative_words)
    if pos > neg:
        sentiment = "Positive"
    elif neg > pos:
        sentiment = "Negative"
    else:
        sentiment = "Neutral"
    print("  " + sentiment + ": " + review[:40] + "...")

Structured data is organized in rows and columns with defined types. Unstructured data like text requires additional processing to extract useful information. This basic sentiment analysis converts unstructured reviews into a structured categorical field (Positive/Negative/Neutral).

Key points

Concepts covered

Numeric Data, Categorical Data, Ordinal Data, Text Data, Structured vs Unstructured Data