如何按指定列组合去重并保留rykkedage最小的行?
Hey Louise, great question! Let's break down how to handle both scenarios you're asking about—first keeping only the row with the smallest rykkedage for duplicate column combinations, then extending that to keep all rows where rykkedage matches the group minimum (even if there are ties). We'll use Python's pandas library since it's the go-to tool for this kind of data cleaning.
1. Keep Rows with the Smallest rykkedage (Single Row per Group)
If you only want one row per duplicate combination (the one with the smallest rykkedage), here's a concise way to do it:
import pandas as pd # Load your data (adjust the file path/format as needed) df = pd.read_csv("your_dataset.csv") # Define the columns that determine a duplicate row duplicate_key = ["ID", "BilagNr", "Henstand", "Aftale", "Belob", "RP", "Pos", "Dps", "Udlign"] # Group by the duplicate key and keep the row with the smallest rykkedage filtered_df = df.loc[df.groupby(duplicate_key)["rykkedage"].idxmin()] # Reset the index for cleaner output (optional) filtered_df = filtered_df.reset_index(drop=True)
How this works:
groupby(duplicate_key)clusters rows that are identical across your specified columns.["rykkedage"].idxmin()finds the index of the row in each group with the smallestrykkedagevalue.loc[...]extracts those specific rows from the original DataFrame.
2. Keep All Rows with the Smallest rykkedage (Including Ties)
If you want to retain all rows where rykkedage equals the group minimum (even if multiple rows share that smallest value), adjust the code like this:
import pandas as pd df = pd.read_csv("your_dataset.csv") duplicate_key = ["ID", "BilagNr", "Henstand", "Aftale", "Belob", "RP", "Pos", "Dps", "Udlign"] # Add a temporary column with the minimum rykkedage for each group df["group_min_rykkedage"] = df.groupby(duplicate_key)["rykkedage"].transform("min") # Filter rows where rykkedage matches the group's minimum filtered_with_ties_df = df[df["rykkedage"] == df["group_min_rykkedage"]] # Remove the temporary column (optional but clean) filtered_with_ties_df = filtered_with_ties_df.drop(columns=["group_min_rykkedage"]) filtered_with_ties_df = filtered_with_ties_df.reset_index(drop=True)
How this works:
transform("min")calculates the smallestrykkedagefor each group and assigns that value to every row in the group (so we can compare individual rows to the group minimum).- The final filter keeps only rows where
rykkedagematches the group's minimum value, preserving all ties.
Example Data & Results
Let's use a sample dataset to show how both solutions work:
Original Data
| ID | BilagNr | Henstand | Aftale | Belob | RP | Pos | Dps | Udlign | rykkedage |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 1001 | H1 | A1 | 500 | R1 | P1 | D1 | U1 | 2 |
| 1 | 1001 | H1 | A1 | 500 | R1 | P1 | D1 | U1 | 1 |
| 1 | 1001 | H1 | A1 | 500 | R1 | P1 | D1 | U1 | 1 |
| 2 | 1002 | H2 | A2 | 300 | R2 | P2 | D2 | U2 | 3 |
| 2 | 1002 | H2 | A2 | 300 | R2 | P2 | D2 | U2 | 2 |
Result: Single Row per Group (Minimum rykkedage)
| ID | BilagNr | Henstand | Aftale | Belob | RP | Pos | Dps | Udlign | rykkedage |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 1001 | H1 | A1 | 500 | R1 | P1 | D1 | U1 | 1 |
| 2 | 1002 | H2 | A2 | 300 | R2 | P2 | D2 | U2 | 2 |
Result: All Tied Minimum Rows
| ID | BilagNr | Henstand | Aftale | Belob | RP | Pos | Dps | Udlign | rykkedage |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 1001 | H1 | A1 | 500 | R1 | P1 | D1 | U1 | 1 |
| 1 | 1001 | H1 | A1 | 500 | R1 | P1 | D1 | U1 | 1 |
| 2 | 1002 | H2 | A2 | 300 | R2 | P2 | D2 | U2 | 2 |
内容的提问来源于stack exchange,提问作者Louise Sørensen

