加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 无法使用灵活类型执行reduce

发布时间:2020-12-16 22:39:09 所属栏目:Python 来源:网络整理
导读:我有这个数据集: Game1 Game2 Game3 Game4 Game5Player1 2 6 5 2 2Player2 6 4 1 8 4Player3 8 3 2 1 5Player4 4 9 4 7 9 我想为每个玩家计算5场比赛的总和. 这是我的代码: import csvf=open('Games','rb')f=csv.reader(f,delimiter=';')lst=list(f)lstimp

我有这个数据集:

           Game1    Game2   Game3   Game4     Game5

Player1       2        6        5       2        2

Player2       6        4        1       8        4

Player3       8        3        2       1        5

Player4       4        9        4       7        9

我想为每个玩家计算5场比赛的总和.

这是我的代码:

import csv
f=open('Games','rb')
f=csv.reader(f,delimiter=';')
lst=list(f)
lst
import numpy as np
myarray = np.asarray(lst)
x=myarray[1,1:] #First player
y=np.sum(x)

我有错误“无法使用灵活类型执行缩减”.我真的很陌生,我需要你的帮助.

谢谢

最佳答案
考虑使用Pandas module:

import pandas as pd

df = pd.read_csv('/path/to.file.csv',sep=';')

结果DataFrame:

In [196]: df
Out[196]:
         Game1  Game2  Game3  Game4  Game5
Player1      2      6      5      2      2
Player2      6      4      1      8      4
Player3      8      3      2      1      5
Player4      4      9      4      7      9

和:

In [197]: df.sum(axis=1)
Out[197]:
Player1    17
Player2    23
Player3    19
Player4    33
dtype: int64

In [198]: df.sum(1).values
Out[198]: array([17,23,19,33],dtype=int64)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读