Learning How to Code
Schema
- What Is Python?
- Understanding Python as a Language
- Why Generative AI Is Helpful for Coding Purposes
- Writing Good Prompts
- Get Familiar with the Environments – Google Colab
Slides
Another Coding Environments
Resources
Activity
Python Scripts
Open CSV
import pandas as pd
file_path = "/content/knoedler.csv"
try:
df = pd.read_csv(file_path, low_memory=False)
print("✓ CSV file loaded successfully!\n")
except FileNotFoundError:
print(f"Error: File '{file_path}' not found.")
print("Download from: https://github.com/thegetty/provenance-index-csv/tree/main/knoedler")
exit()
Read column names
print("=" * 60)
print("COLUMN NAMES")
print("=" * 60)
columns = df.columns.tolist()
for i, col in enumerate(columns, 1):
print(f" {i}. {col}")
print(f"\nTotal columns: {len(columns)}\n")
Check number of rows
print("=" * 60)
print("DATASET DIMENSIONS")
print("=" * 60)
num_rows = len(df)
num_cols = len(df.columns)
print(f" Number of rows: {num_rows:,}")
print(f" Number of columns: {num_cols}")
print(f" Total cells: {num_rows * num_cols:,}\n")
Unique values in a column
print("=" * 60)
print("UNIQUE VALUES ANALYSIS")
print("=" * 60)
# Function to get unique values for any column
def get_unique_values(dataframe, column_name):
"""Return unique values and count for a specified column."""
if column_name not in dataframe.columns:
print(f"Column '{column_name}' not found in dataset.")
return None
unique_vals = dataframe[column_name].dropna().unique()
unique_count = len(unique_vals)
print(f"\nColumn: '{column_name}'")
print(f" Unique values: {unique_count:,}")
print(f" Missing values: {dataframe[column_name].isna().sum():,}")
# Show sample of unique values (first 10)
if unique_count > 0:
print(f" Sample values (up to 10):")
for val in list(unique_vals)[:10]:
print(f" - {val}")
return unique_vals
# Analyze a few columns (adjust column names based on actual data)
# Common columns in Knoedler data: 'artist_name', 'title', 'genre', 'sale_date'
sample_columns = columns[:3] # Analyze first 3 columns as example
for col in sample_columns:
get_unique_values(df, col)
Save a CSV (filtered or processed data)
print("\n" + "=" * 60)
print("SAVING PROCESSED DATA")
print("=" * 60)
# Example: Save a summary of missing data per column
missing_data = pd.DataFrame({
'column': df.columns,
'missing_count': df.isnull().sum().values,
'missing_percent': (df.isnull().sum().values / len(df) * 100).round(2),
'unique_values': [df[col].nunique() for col in df.columns]
})
output_file = "provenance_data_summary.csv"
missing_data.to_csv(output_file, index=False)
print(f"✓ Summary saved to '{output_file}'\n")
# Optional: Save filtered subset
# Example: Save rows where a specific column has missing data
# df_with_gaps = df[df['artist_name'].isna()]
# df_with_gaps.to_csv("records_missing_artist.csv", index=False)
print("=" * 60)
print("SCRIPT COMPLETE")
print("=" * 60)© 2025 Coding Provenance Workshop — Licensed under a Creative Commons Attribution 4.0 International License.
Reuse is encouraged — we happily promote changes of custody. Just be sure to write a good provenance record and cite this workshop.