7.5 转置
现有学生成绩表如下:
1. 将其扩展为 STUDENTID,SUBJECT,SCORE 的成绩表(列转行)。
2. 完成 1 中的逆运算(行转列)。
SPL
A | B | |
---|---|---|
1 | =file(“SCORES.csv”).import@tc() | |
2 | =A1.fname().to(2,) | |
3 | =A1.news(A2;STUDENTID,~:SUBJECT,A1.~.field(~):SCORE) | /news() 列转行 |
4 | =A1.pivot@r(STUDENTID;SUBJECT,SCORE;${A2.concat@c()}) | /pivot@r() 列转行 |
5 | =A3.group(STUDENTID;${A3.id(SUBJECT).(“~.select@1(SUBJECT=="”+~+“").SCORE:”+~).concat@c()}) | /group() 行转列 |
6 | =A3.pivot(STUDENTID;SUBJECT,SCORE) | /pivot() 行转列 |
SPL 中 group()是分组函数可以实现行转列运算,news() 是逆分组函数可以实现列转行运算,两者互为逆运算。SPL 还提供了 pivot()和 pivot@r(),使用它们可以方便的进行行列互转运算。
SQL
1. 列转行
SELECT * FROM (
SELECT STUDENTID, English, Math, PE
FROM scores)
UNPIVOT (
SCORE FOR SUBJECT IN (English, Math, PE));
2. 行转列
WITH inv_scores AS (
SELECT * FROM (
SELECT STUDENTID, English, Math, PE
FROM scores)
UNPIVOT (
SCORE FOR SUBJECT IN (English, Math, PE)))
SELECT * FROM (
SELECT STUDENTID, SUBJECT, SCORE
FROM inv_scores)
PIVOT (
AVG(SCORE) FOR SUBJECT IN ('ENGLISH' AS English, 'MATH' AS Math, 'PE' AS PE))
ORDER BY STUDENTID;
Python
score_data=pd.read_csv("../SCORES.csv")
clm=score_data.columns[1:]
subject_score=score_data.melt(id_vars="STUDENTID",
value_vars=clm,var_name='SUBJECT',value_name="SCORE") #列转行
scores=subject_score.pivot(index='STUDENTID',columns='SUBJECT',values='SCORE') #行转列
Python 提供了 pivot()和 melt() 方法来实现行列互转运算。