pandas.DataFrame.melt()
をお使い下さい
https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.melt.html#pandas.DataFrame.melt
Python
1import pandas as pd
2from pandas import DataFrame,Series
3
4#score
5data={
6"名前":["浅田","今川","宇佐","江田","小野","加納","岸辺","倉田","剣持"],
7"こくご":["79","75","49","99","96","94","48","21","88"],
8"さんすう":["3","70","89","47","29","88","17","67","8"],
9"えいご":["33","22","65","5","36","45","19","100","36"]
10}
11
12#データフレームscore
13score = DataFrame(data)
14ret = score.melt(id_vars='名前', var_name='科目名', value_name='点数')
15print(ret)
16# 名前 科目名 点数
17#0 浅田 こくご 79
18#1 今川 こくご 75
19#2 宇佐 こくご 49
20#3 江田 こくご 99
21#4 小野 こくご 96
22#5 加納 こくご 94
23#6 岸辺 こくご 48
24#7 倉田 こくご 21
25#8 剣持 こくご 88
26#9 浅田 さんすう 3
27#10 今川 さんすう 70
28#11 宇佐 さんすう 89
29#12 江田 さんすう 47
30#13 小野 さんすう 29
31#14 加納 さんすう 88
32#15 岸辺 さんすう 17
33#16 倉田 さんすう 67
34#17 剣持 さんすう 8
35#18 浅田 えいご 33
36#19 今川 えいご 22
37#20 宇佐 えいご 65
38#21 江田 えいご 5
39#22 小野 えいご 36
40#23 加納 えいご 45
41#24 岸辺 えいご 19
42#25 倉田 えいご 100
43#26 剣持 えいご 36
更に、出力を名前順にしたい場合は、DataFrame.sort_values()
でソートします。
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.sort_values.html
Python
1ret = ret.sort_values('名前', ignore_index=True)
【ふりがなを付けた例】
Python
1import pandas as pd
2from pandas import DataFrame,Series
3
4#score
5data={
6"名前":["浅田","今川","宇佐","江田","小野","加納","岸辺","倉田","剣持"],
7"ふりがな":["あさだ","いまがわ","うさ","えだ","おの","かのう","きしべ","くらた","けんもち"],
8"こくご":["79","75","49","99","96","94","48","21","88"],
9"さんすう":["3","70","89","47","29","88","17","67","8"],
10"えいご":["33","22","65","5","36","45","19","100","36"]
11}
12
13#データフレームscore
14score = DataFrame(data)
15ret = score.melt(id_vars=['名前','ふりがな'], var_name='科目名', value_name='点数').sort_values('ふりがな', ignore_index=True)
16print(ret)
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/05/17 12:30
2020/05/17 12:46 編集
2020/05/17 13:12