如何按年度与受援国汇总promised aid和provided aid的总金额?
Hey there! Let's walk through how to calculate those yearly aid totals per country—this is a standard aggregation task, and there are straightforward solutions depending on the tool you're using. I'll cover the most common options below:
If you're working with a spreadsheet, a Pivot Table is the easiest way to get this done:
- First, select your entire dataset (including the header row)
- Go to the Insert tab and click PivotTable, then choose where you want the result to appear
- In the PivotTable Fields pane:
- Drag
yearandrcodeinto the Rows area (this will group your data by year first, then country) - Drag
promised aidandprovided aidinto the Values area - Double-check the aggregation method: by default, it might use "Count"—click the value field, select Value Field Settings, and switch to Sum
- Drag
- You’ll end up with a table that shows exactly what you need: e.g., 2002 → Afghanistan → total promised aid = 20M, total provided aid = 18M.
If you're using Python for data analysis, Pandas makes this a breeze (plus a filter if you need to limit to 2002-2012):
import pandas as pd # Load your data into a DataFrame (adjust the path/loading method as needed) df = pd.read_csv("your_aid_data.csv") # Filter to only 2002-2012 if your dataset includes other years filtered_df = df[(df["year"] >= 2002) & (df["year"] <= 2012)] # Group by year and country code, then sum the two aid columns summary_df = filtered_df.groupby(["year", "rcode"])[["promised aid", "provided aid"]].sum().reset_index() # View the first few rows of the result print(summary_df.head())
- The
groupbymethod clusters your data by year andrcode,sum()calculates the total for each aid type, andreset_index()turns the grouped columns back into regular columns for easier viewing and further analysis.
For R users, the dplyr package provides a clean, readable workflow for this task:
library(dplyr) # Load your data (adjust the file path or loading function as needed) df <- read.csv("your_aid_data.csv") # Filter to 2002-2012, group by year and country, then compute totals summary_df <- df %>% filter(year >= 2002 & year <= 2012) %>% group_by(year, rcode) %>% summarise( total_promised = sum(`promised aid`, na.rm = TRUE), total_provided = sum(`provided aid`, na.rm = TRUE), .groups = "drop" ) # Preview the result head(summary_df)
filter()narrows down the dataset to your target year range,group_by()sets up the grouping structure, andsummarise()calculates the total aid values. Thena.rm = TRUEparameter ensures missing values don't break your sum calculations, and.groups = "drop"returns a regular data frame instead of a grouped one.
A quick tip: If your dataset has missing values in the aid columns, all the methods above include ways to ignore blank/NaN cells so they don't skew your totals.
内容的提问来源于stack exchange,提问作者nikip

