Given 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 09:02:27 浏览: 53
利用Python和Pandas进行学生成绩处理.pdf
5星 · 资源好评率100%
Here's the code to filter out students with scores >= 80 and add a new column showing their grade level:
```python
import pandas as pd
# Read the input file as DataFrame
df = pd.read_csv('1.txt', sep='\s+')
# Filter out students with scores >= 80
df = df[df['score'] >= 80]
# Add a new column for grade level
def get_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'
df['grade'] = df['score'].apply(get_grade)
# Print the resulting DataFrame
print(df)
```
Output:
```
studentid score grade
0 student1 95 A
1 sutdent2 88 B
5 student5 82 B
```
阅读全文