Instalar com Codex ou Claude Copie este prompt, cole no Codex, Claude ou outro assistente e deixe que ele revise a página da skill e instale para você.
Um comando direto ignora o prompt de revisão. Verifique a origem antes de executá-lo.
# Named aggregations (pandas 2.0+)
result = df.groupby('category').agg(
total_sales=('sales', 'sum'),
avg_price=('price', 'mean'),
count=('id', 'count')
)
# Transform for broadcasting back to original shape
df['pct_of_group'] = df.groupby('category')['value'].transform(
lambda x: x / x.sum()
)
Index Operations
# Set index for frequent lookups
df = df.set_index('user_id')
user_data = df.loc[12345] # O(1) lookup# Reset before groupby if index not needed
df.reset_index(drop=True, inplace=True)
# Multi-index for hierarchical data
df = df.set_index(['region', 'date'])
df.loc[('US', '2024-01')] # Hierarchical access
Memory Reduction Recipe
defreduce_memory(df: pd.DataFrame) -> pd.DataFrame:
"""Reduce DataFrame memory by 50-90%."""for col in df.columns:
col_type = df[col].dtype
if col_type == 'object':
if df[col].nunique() / len(df) < 0.5:
df[col] = df[col].astype('category')
elif col_type == 'int64':
if df[col].min() >= 0:
if df[col].max() < 255:
df[col] = df[col].astype('uint8')
elif df[col].max() < 65535:
df[col] = df[col].astype('uint16')
else:
if df[col].min() > -128and df[col].max() < 127:
df[col] = df[col].astype('int8')
elif df[col].min() > -32768and df[col].max() < 32767:
df[col] = df[col].astype('int16')
elif col_type == 'float64':
df[col] = df[col].astype('float32')
return df
Parquet Over CSV
# Save with compression
df.to_parquet('data.parquet', compression='snappy', index=False)
# Read specific columns (predicate pushdown)
df = pd.read_parquet('data.parquet', columns=['id', 'value'])
# Partitioned writes for large datasets
df.to_parquet(
'data/',
partition_cols=['year', 'month'],
compression='snappy'
)
# Sort before merge for performance
left = left.sort_values('key')
right = right.sort_values('key')
result = pd.merge(left, right, on='key')
# Use categorical keys for memory efficiencyfor df in [left, right]:
df['key'] = df['key'].astype('category')
Query vs Boolean Indexing
# Boolean indexing - standard
filtered = df[(df['status'] == 'active') & (df['value'] > 100)]
# query() - more readable for complex conditions
filtered = df.query('status == "active" and value > 100')
# query() with variables
min_val = 100
filtered = df.query('value > @min_val')
JSON Flattening
Normalize nested JSON structures into tabular format.