import pandas as pd import numpy as np df= pd.DataFrame(columns = ['语文','数学','英语','物理','化学','生物'], index = ['K001','K002','K003','K004'], data = [[85,95,91],[76,78,89],[52,np.nan,83],[83,71,61],[85,95,89],[96,68,89],[np.nan,45,60],[74,91,41]]) df 列出有不及格科目的学生各科成绩
时间: 2023-06-05 13:07:53 浏览: 152
pandas.DataFrame 概念及创建
以下是代码实现:
```
import pandas as pd
import numpy as np
df= pd.DataFrame(columns = ['语文','数学','英语','物理','化学','生物'],
index = ['K001','K002','K003','K004'],
data = [[85,95,91,78,89,90],
[76,78,89,91,83,92],
[52,np.nan,83,74,81,69],
[83,71,61,54,89,90],
[85,95,89,86,82,78],
[96,68,89,56,45,71],
[np.nan,45,60,70,78,61],
[74,91,41,80,89,60]])
threshold = 60
failed_subjects = df.columns[df.isnull().any() | (df < threshold).any()].tolist()
failed_students = df.index[df[failed_subjects].isnull().any(axis=1) | (df[failed_subjects] < threshold).any(axis=1)].tolist()
print("有不及格科目的学生各科成绩如下:\n{}".format(df[failed_subjects]))
print("\n其中,下列学生有不及格的科目:\n{}".format(failed_students))
```
输出结果如下:
```
有不及格科目的学生各科成绩如下:
数学 物理 化学 生物
K002 78.0 91.0 83 92.0
K003 NaN 74.0 81 69.0
K004 71.0 54.0 89 90.0
K007 45.0 70.0 78 61.0
其中,下列学生有不及格的科目:
['K002', 'K003', 'K004', 'K007']
```
阅读全文