|
发表于 2020-10-9 13:34:14
|
显示全部楼层
- import pandas as pd
- a = pd.DataFrame([['A', '张三', '优', '优'],
- ['B', '张三', '优', '良'],
- ['C', '张三', '良', '良'],
- ['A', '李四', '优', '优'],
- ['B', '李四', '良', '优']],
- columns=['teacher', 'student', 'math', 'English'])
- print(a)
- result = pd.DataFrame(data=None, index=['张三', '李四'], columns=['math', 'English'])
- for j in ['math', 'English']:
- b = pd.pivot_table(data=a, values=['teacher'], index=['student'], columns=[j], aggfunc='count')
- b.columns = ['优', '良']
- for i in b.index:
- if b.loc[i, '优'] > 1:
- result.loc[i, j] = '优'
- elif b.loc[i, '良'] > 1:
- result.loc[i, j] = '良'
- elif b.loc[i, '优'] == b.loc[i, '良']:
- result.loc[i, j] = 'FLASE'
- print(result)
复制代码 |
|