You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Pandas DataFrame匹配项在第二个DataFrame创建聚合值列

Got it, let's break down how to build that aggregated DataFrame using pandas—since your real dataset has more variable types, I’ll cover flexible approaches that work across different column types too.

First, let's start by recreating your sample DataFrame so we can work with concrete code:

import pandas as pd

data = {
    'id': [1,2,3,4,5,6],
    'country': ['US','US','US','US','US','US'],
    'state': ['TX','CA','CA','TX','NY','TX'],
    'amt1': [20,30,40,80,20,10],
    'amt2': [40,900,230,670,120,80],
    'var1': ['a','b','c','d','a','c'],
    'var2': ['X','Y','X','X','Y','X']
}

df1 = pd.DataFrame(data)

1. Basic aggregation by a single column

Let's say you want to group by state, sum the numeric columns (amt1, amt2), and count how many records are in each state:

agg_df_single = df1.groupby('state').agg(
    total_amt1=('amt1', 'sum'),
    total_amt2=('amt2', 'sum'),
    record_count=('id', 'count')
).reset_index()

print(agg_df_single)

This will output:

statetotal_amt1total_amt2record_count
CA7011302
NY201201
TX1107903

2. Multi-column grouping with mixed aggregation functions

If you need to group by multiple columns (e.g., state + var2) and apply different functions to different columns (like summing one, averaging another, and counting unique values in a categorical column):

agg_df_multi = df1.groupby(['state', 'var2']).agg(
    sum_amt1=('amt1', 'sum'),
    avg_amt2=('amt2', 'mean'),
    unique_var1_count=('var1', pd.Series.nunique)
).reset_index()

print(agg_df_multi)

Output:

statevar2sum_amt1avg_amt2unique_var1_count
CAX40230.01
CAY30900.01
NYY20120.01
TXX110263.3333

3. Custom aggregation functions

For more complex logic (like calculating a ratio between two columns per group), you can define your own function:

# Custom function: Calculate average of (amt1/amt2) for each group
def avg_amt_ratio(group):
    return (group['amt1'] / group['amt2']).mean()

agg_df_custom = df1.groupby('state').agg(
    total_amt1=('amt1', 'sum'),
    avg_amt1_amt2_ratio=('amt1', avg_amt_ratio)  # Use any column as a placeholder; function accesses the whole group
).reset_index()

print(agg_df_custom)

Tips for your real dataset

  • For categorical columns (like var1, var2), common aggregations include nunique (count unique values), first/last (get first/last value in the group), or mode (most frequent value).
  • For date columns (if you have them), use min/max to get the earliest/latest date in the group.
  • If you need to keep non-aggregated columns alongside results, use transform instead of agg to broadcast aggregated values back to the original row count.

内容的提问来源于stack exchange,提问作者cod_rg567

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:29:52