python 29 lines · 1 tab

pandas DataFrame essentials: loading, indexing, and selection

1 tab
import pandas as pd

df = pd.read_csv(
    'orders.csv',
    parse_dates=['created_at'],
    dtype={
        'customer_id': 'int64',
        'country': 'string',
        'status': 'category',
    },
)

df.columns = (
    df.columns.str.strip()
    .str.lower()
    .str.replace(' ', '_', regex=False)
)

active_orders = df.loc[
    (df['status'] == 'paid') &
    (df['country'].isin(['US', 'CA'])),
    ['customer_id', 'created_at', 'total_amount']
].copy()

active_orders = active_orders.set_index('created_at').sort_index()
latest_orders = active_orders.last('30D')

print(latest_orders.head())
print(latest_orders.dtypes)
1 file · python Explain with highlit

I treat pandas as the default workbench for structured data. The goal is to make loading explicit, indexes predictable, and selection operations readable under maintenance pressure. I prefer stable column naming, typed parsing for dates, and avoiding chained indexing. Once a DataFrame is shaped well, downstream feature engineering and model training get dramatically simpler.


Related snips

python
import os
import stat

for root, _dirs, files in os.walk('/etc'):
    for name in files:
        path = os.path.join(root, name)

Python security audit script for exposed risky filesystem state

python auditing host-security
by Kai Nakamura 1 tab
python
class Product(models.Model):
    name = models.CharField(max_length=200)
    slug = models.SlugField(blank=True)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    cost = models.DecimalField(max_digits=10, decimal_places=2)
    margin = models.DecimalField(max_digits=5, decimal_places=2, blank=True)

Django model signals vs overriding save

django python models
by Priya Sharma 2 tabs
python
from django.urls import path
from . import views

app_name = 'blog'

urlpatterns = [

Django URL namespacing and reverse lookups

django python urls
by Priya Sharma 3 tabs
python
import graphene
from graphene_django import DjangoObjectType
from blog.models import Post, Comment


class PostType(DjangoObjectType):

Django GraphQL with Graphene

django python graphql
by Priya Sharma 2 tabs
ruby
class AddSettingsToAccounts < ActiveRecord::Migration[7.1]
  disable_ddl_transaction!

  def change
    add_column :accounts, :settings, :jsonb, null: false, default: {}

Postgres JSONB Partial Index for Feature Flags

rails postgres jsonb
by codesnips 3 tabs
python
from django.db.models import Count, Avg, Sum, Q, F
from django.views.generic import TemplateView
from products.models import Product, Order, OrderItem


class DashboardView(TemplateView):

Django aggregation with annotate for statistics

django python database
by Priya Sharma 1 tab

Share this code

Here's the card — post it anywhere.

pandas DataFrame essentials: loading, indexing, and selection — share card
Link copied