input is the path name of the file, which contain a dataframe,you need to filter out all students with scores >= 80 and add a column showing their grade level >=90 points, A grade; 80-89, B grade; 70-79, C grade; 60-69, D grade; 0-59, E grade. Example Input 1.txt Output studentid score grade 0 student1 95 A 1 sutdent2 88 B 4 student5 82 B content in file 1.txt: 1studentid score 2student1 95 3sutdent2 88 4student3 75 5student4 60 6student5 82 use pandas in python
时间: 2024-01-25 20:02:27 浏览: 60
利用Python和Pandas进行学生成绩处理.pdf
5星 · 资源好评率100%
Sure, I can help you with that! Here is the code to read the file, filter the students with scores >= 80, and add a new column showing their grade level:
```python
import pandas as pd
# Read the file
df = pd.read_csv('input.txt', sep=' ')
# Filter the students with scores >= 80
df = df[df['score'] >= 80]
# Define a function to assign grades
def assign_grade(score):
if score >= 90:
return 'A'
elif score >= 80:
return 'B'
elif score >= 70:
return 'C'
elif score >= 60:
return 'D'
else:
return 'E'
# Add a new column showing the grade level
df['grade'] = df['score'].apply(assign_grade)
# Print the output
print(df)
```
The output will be:
```
studentid score grade
0 student1 95 A
1 sutdent2 88 B
5 student5 82 B
```
Please note that you need to modify the file path in the code to match the actual path of your input file.
阅读全文