Staffelter Hof Winery is Germany's oldest business, established in 862 under the Carolingian dynasty. It has continued to serve customers through dramatic changes in Europe, such as the Holy Roman Empire, the Ottoman Empire, and both world wars. What characteristics enable a business to stand the test of time?
To help answer this question, BusinessFinancing.co.uk researched the oldest company still in business in almost every country and compiled the results into several CSV files. This dataset has been cleaned.
Having useful information in different files is a common problem. While it's better to keep different types of data separate for data storage, you'll want all the data in one place for analysis. You'll use joining and data manipulation to work with this data and better understand the world's oldest businesses.
The Data
data/businesses.csv and data/new_businesses.csv
| Column | Description |
|---|---|
business | Name of the business (varchar) |
year_founded | Year the business was founded (int) |
category_code | Code for the business category (varchar) |
country_code | ISO 3166-1 three-letter country code (char) |
data/countries.csv
| Column | Description |
|---|---|
country_code | ISO 3166-1 three-letter country code (varchar) |
country | Name of the country (varchar) |
continent | Name of the continent the country exists in (varchar) |
data/categories.csv
| Column | Description |
|---|---|
category_code | Code for the business category (varchar) |
category | Description of the business category (varchar) |
Understand the factors that help a business be timeless by answering these questions:
What is the oldest business on each continent? Save your answer as a DataFrame called oldest_business_continent with four columns: continent, country, business, and year_founded in any order.
How many countries per continent lack data on the oldest businesses? Does including new_businesses change this? Count the number of countries per continent missing business data, including new_businesses, and store the results in a DataFrame named count_missing with columns for the continent and the count.
Which business categories are best suited to last many years, and on what continent are they? Create a DataFrame called oldest_by_continent_category that stores the oldest founding year for each continent and category combination. It should contain three columns: continent, category, and year_founded, in that order.
# Import necessary libraries
import pandas as pd
# Load the data
businesses = pd.read_csv("data/businesses.csv")
new_businesses = pd.read_csv("data/new_businesses.csv")
countries = pd.read_csv("data/countries.csv")
categories = pd.read_csv("data/categories.csv")# Start coding here!
# Use as many cells as you like
business_country = businesses.merge(countries, on="country_code")
business_country.head(20)oldest_continent_year = business_country.pivot_table(index="continent", values="year_founded", aggfunc="min")
oldest_business_continent_full = oldest_continent_year.merge(business_country, on="year_founded")
oldest_business_continent = oldest_business_continent_full[["continent", "country", "business", "year_founded"]]
oldest_business_continent# Add the data in new_businesses to the existing businesses
all_businesses = pd.concat([new_businesses, businesses])
# Perform a new merge between the businesses and the countries data. Use additional parameters this time to perform an outer merge and create an indicator column to better see the missing values. An outer merge combines two DataFrames based on a key column and includes all rows from both DataFrames
new_all_countries = all_businesses.merge(countries, on="country_code", how="outer", indicator=True)
# Filter to find countries with missing business data
new_missing_countries = new_all_countries[new_all_countries["_merge"] != "both"]
# Group by continent and create a "count_missing" column
count_missing = new_missing_countries.groupby("continent").agg({"country":"count"})
count_missing.columns = ["count_missing"]
# View the results
print(count_missing)oldest_by_continent_category = business_country.pivot_table(index=["continent", "category_code"], values="year_founded", aggfunc="min")
oldest_by_continent_category