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. Sample Input 5 12213101 85 3.5 12213103 90 4.0 12213212 80 1.2 12213129 87 0.5 12213301 93 2.1 12213103 4.44 Output: 12213103 4.44 Constraints: The input dataframe will have at most 10^5rows. The student_idcolumn will contain unique non-negative integers. The base and bonus columns will contain non-negative float point numbers. Note: You can use the mean() method to compute the mean of a column.use pandas in python
时间: 2024-01-11 09:05:41 浏览: 93
a data base for a student
Sure, I can help you with that. Here's a snippet of Python code that should do the trick using pandas:
```
import pandas as pd
# read in the input dataframe
n = int(input())
data = []
for i in range(n):
row = input().split()
data.append({
'student_id': int(row[0]),
'base': float(row[1]),
'bonus': float(row[2])
})
df = pd.DataFrame(data)
# calculate the average base and bonus
avg_base = df['base'].mean()
avg_bonus = df['bonus'].mean()
# filter out rows that don't meet the criteria
df = df[(df['base'] > avg_base) & (df['bonus'] >= avg_bonus)]
# calculate the bonus percentage and sort by student_id
df['bonus_percentage'] = df['bonus'] / df['base'] * 100
df = df.sort_values(by='student_id')
# output the filtered rows, or "None" if there are none
if len(df) > 0:
for index, row in df.iterrows():
print(row['student_id'], '{:.2f}'.format(row['bonus_percentage']))
else:
print('None')
```
You can copy and paste this code into your Python environment and run it to test it out with your input data. Let me know if you have any questions or need further assistance!
阅读全文