admin管理员组文章数量:1336331
I have a DataFrame with 3 column of zeroes and ones corresponding to 3 different classes. I want to get a single series of zeroes, ones, and twos depending of the class of the entry (0 for the first class, 1 for the second one and 2 for the third one):
>>> results.head()
HOME_WINS DRAW AWAY_WINS
ID
0 0 0 1
1 0 1 0
2 0 0 1
3 1 0 0
4 0 1 0
What I want :
>>> results.head()
SCORE
ID
0 2
1 1
2 2
3 0
4 1
I have a DataFrame with 3 column of zeroes and ones corresponding to 3 different classes. I want to get a single series of zeroes, ones, and twos depending of the class of the entry (0 for the first class, 1 for the second one and 2 for the third one):
>>> results.head()
HOME_WINS DRAW AWAY_WINS
ID
0 0 0 1
1 0 1 0
2 0 0 1
3 1 0 0
4 0 1 0
What I want :
>>> results.head()
SCORE
ID
0 2
1 1
2 2
3 0
4 1
Share
Improve this question
asked Nov 20, 2024 at 20:11
Noé MastrorilloNoé Mastrorillo
3091 silver badge12 bronze badges
2 Answers
Reset to default 5Multiply by a dictionary, sum
and convert to_frame
:
d = {'HOME_WINS': 0, 'DRAW': 1, 'AWAY_WINS': 2}
out = df.mul(d).sum(axis=1).to_frame(name='SCORE')
Or using a dot
product:
d = {'HOME_WINS': 0, 'DRAW': 1, 'AWAY_WINS': 2}
out = df.dot(pd.Series(d)).to_frame(name='SCORE')
Or, if there is exactly one 1
per row, with from_dummies
:
d = {'HOME_WINS': 0, 'DRAW': 1, 'AWAY_WINS': 2}
out = pd.from_dummies(df)[''].map(d).to_frame(name='SCORE')
Output:
SCORE
ID
0 2
1 1
2 2
3 0
4 1
Another possible solution, whose steps are:
Using
idxmax(axis=1)
, the function determines the column name with the highest value (which is1
in ourcase) for each row.The resulting series is then mapped to numerical scores using
map
with the dictionaryd = {'HOME_WINS': 0, 'DRAW': 1, 'AWAY_WINS': 2}
Finally,
to_frame('Score')
converts the into a dataframe with a single column namedScore
d = {'HOME_WINS': 0,'DRAW': 1, 'AWAY_WINS': 2}
df.idxmax(axis=1).map(d).to_frame('Score')
Output:
Score
ID
0 2
1 1
2 2
3 0
4 1
版权声明:本文标题:Get a single series of classes instead of one series for each class with pandas in Python - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742331900a2454861.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论