在Pandas中查找指定区间内某列的最小值
To calculate the minimum GDP within a specific year or year range, you’ll first filter your DataFrame to include only the rows matching your desired years, then compute the minimum of the GDP column. Here’s how to do it for your use cases:
1. Minimum GDP for a Single Year (e.g., 1955)
Filter the DataFrame to keep only rows where the Year equals 1955, then extract the GDP column and call .min():
# Filter for 1955 and get the minimum GDP min_gdp_1955 = df[df['Year'] == 1955]['GDP'].min() print(min_gdp_1955) # Output: 109967
2. Minimum GDP for a Year Range (e.g., 1955-1956)
For a range of years, you have two straightforward options:
Option 1: Use .isin() for specific years
This works great if you want to pick non-consecutive years too:
# Filter for 1955 and 1956, then get the minimum GDP min_gdp_1955_1956 = df[df['Year'].isin([1955, 1956])]['GDP'].min() print(min_gdp_1955_1956) # Output: 109967
Option 2: Use .between() for consecutive years
If your range is a continuous block of years, .between() is a clean way to define the interval:
# Filter years between 1955 and 1956 (inclusive), then get the minimum GDP min_gdp_range = df[df['Year'].between(1955, 1956)]['GDP'].min() print(min_gdp_range) # Output: 109967
Bonus: Find the full row with the minimum value
If you want to see exactly which quarter/year has the minimum GDP in your range, use .idxmin() to get the index of the minimum value, then fetch the full row:
# Get the row with the minimum GDP in 1955-1956 filtered_df = df[df['Year'].between(1955, 1956)] min_row = filtered_df.loc[filtered_df['GDP'].idxmin()] print(min_row) # Output: # Year 1955 # Quarter Q1 # GDP 109967 # Name: 0, dtype: object
Content of the question originates from Stack Exchange, asked by Will

