write python code,please:CS305 is a hard course, but luckly the nice professor provided chances to get bonus points. At the end of the semester, the professor want to check the grades of all the students, and want to pick up top students to invite them become his graduate student. You will be given a dataframe with three columns: student_id, base, and bonus. You should calculate the bonus_percentage column, which contains the percentage of each student's bonus relative to their base grade. The result should be sorted in ascending order by student_id. Additionally, the result should only include students if they have a base that is greater than the average base of all students, and if their bonus is at least the average bonus of all students. Input The first line is a number n, indicating the number of students. The next n lines give the students' information (student_id, base, bonus). Output For each filtered out rows, print the student_id and bonus_percentage (output to 2 decimal places). If there is no row to display, print None.
时间: 2023-12-25 12:02:34 浏览: 31
Here's the Python code to solve the problem:
```python
import pandas as pd
# read input
n = int(input())
data = []
for i in range(n):
student_id, base, bonus = input().split()
data.append([student_id, float(base), float(bonus)])
df = pd.DataFrame(data, columns=['student_id', 'base', 'bonus'])
# calculate bonus_percentage
df['bonus_percentage'] = df['bonus'] / df['base'] * 100
# filter out rows
avg_base = df['base'].mean()
avg_bonus = df['bonus'].mean()
filtered_df = df[(df['base'] > avg_base) & (df['bonus'] >= avg_bonus)]
# sort by student_id and print result
if len(filtered_df) == 0:
print("None")
else:
filtered_df = filtered_df.sort_values(by='student_id')
for index, row in filtered_df.iterrows():
print(row['student_id'], "{:.2f}".format(row['bonus_percentage']))
```
The code uses the pandas library to read the input and manipulate the data as a dataframe. It calculates the bonus_percentage column by dividing the bonus by the base and multiplying by 100. It then filters out the rows that meet the criteria (base > average_base and bonus >= average_bonus) and sorts the result by student_id. Finally, it prints out the filtered rows, or "None" if there are no rows to display.