I would use pandas for data aggregation as pandas is great at data manipulation. I didn't try for long but I struggled to get pandas to stop treating the first column as an index after the aggregation and the 'num3' got stuck in the first column. Other than that, the result is what you are looking for albeit in an order other than what you specified.
Make the dataframe, aggregate the data, rename and re-order the columns, print it csv style:
Edit: I managed to realize where I was going wrong, and recreated a dataframe with the new csv style and got the columns ordered how you were looking for them. As for the row level sorting, I left that alone and assume it is good enough.
import pandas as pd
from io import StringIO
csv_data = """id,num1,num2,num3
1,300,200,1121
2,300,190,1122
3,300,180,1123
4,300,170,1124
5,300,160,1125
6,300,150,1126
7,300,140,1127
8,300,130,1128
9,300,120,1129
10,300,195,1122
11,300,185,1122
12,300,175,1126
13,300,165,1122
14,300,155,1122
15,300,145,1122
16,300,135,1122"""
df = pd.read_csv(StringIO(csv_data)) # make the data frame
# print(df)
# aggregate results 'column : aggregate type'
df = df.groupby('num3').agg({
'id' : 'min',
'num1': 'max',
'num2': 'max',
'num3': 'max',
'num3': 'count',
})
df.columns = ['id', 'num1', 'num2', 'num3_count'] # rename the columns
# print(df)
# reorder the columns as they are stored to a csv style
df = df.to_csv(header = True, columns = ['id', 'num1', 'num2', 'num3_count'])
# recreate the dataframe from the new csv
df = pd.read_csv(StringIO(df))
# reorder the final output as a csv
df = df.to_csv(index = False, header = True, columns = ['id', 'num1', 'num2', 'num3', 'num3_count'])
print(df) # print the dataframe to console for viewing
Returns:
id,num1,num2,num3,num3_count
1,300,200,1121,1
2,300,195,1122,7
3,300,180,1123,1
4,300,170,1124,1
5,300,160,1125,1
6,300,175,1126,2
7,300,140,1127,1
8,300,130,1128,1
9,300,120,1129,1
num3=1122. Which of those 7 rows do you keep for the output? They all have differentnum2. The highestnum2? Or the first row withnum3=1122? Or what? Edit the question and add the clarification.