Last active
July 17, 2026 09:16
-
-
Save ckhung/f3a4f3a88f878181776cc45b2164de79 to your computer and use it in GitHub Desktop.
Select one row per model at the said percentile of price and print out price*1e6 and group size for each group
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import argparse, math | |
| import pandas as pd | |
| def main(): | |
| parser = argparse.ArgumentParser( | |
| description='Select one row per model at the said percentile of price and print out price*1e6 and group size for each group', | |
| formatter_class=argparse.ArgumentDefaultsHelpFormatter) | |
| parser.add_argument( '-p', '--percentile', type=float, default=25.0, | |
| help='Percentile (0-100)') | |
| parser.add_argument('csvfn', help='Input CSV filename') | |
| args = parser.parse_args() | |
| if not (0 <= args.percentile <= 100): | |
| parser.error('--percentile must be between 0 and 100') | |
| df = pd.read_csv(args.csvfn) | |
| df['Price'] = pd.to_numeric(df['Price'], errors='coerce') | |
| result = [] | |
| for model, group in df.groupby('Model', sort=False): | |
| # print(model, len(group)) | |
| g = group.sort_values('Price', kind='stable').reset_index() | |
| pos = math.floor((len(g) - 1) * args.percentile / 100) | |
| row = df.loc[g.loc[pos, "index"]].copy() | |
| row.iloc[-1] = len(group) | |
| result.append(row) | |
| result = pd.DataFrame(result) | |
| result.drop(columns=['Tier'], inplace=True) | |
| result.rename(columns={df.columns[-1]: 'Count'}, inplace=True) | |
| result['Price'] = (result['Price']*1e6).round().astype(int) | |
| print(result.to_csv(index=False), end='') | |
| if __name__ == '__main__': | |
| main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment