Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用pandas访问单个数据帧单元格中的原始数据?_Python_Python 3.x_Pandas - Fatal编程技术网

Python 如何使用pandas访问单个数据帧单元格中的原始数据?

Python 如何使用pandas访问单个数据帧单元格中的原始数据?,python,python-3.x,pandas,Python,Python 3.x,Pandas,我正在使用CSV文件作为输入,开发一个自定义elo/团队评级计算器。在Excel中使用openpyxl时,我可以得到类似的逻辑,但现在我正尝试在pandas中实现它,以便更好地与jupyter和matplotlib集成。但是,我在数据帧中的单个单元格上运行计算时遇到问题 def find_team_row(team_name): switcher = { '100T': 0, 'C9': 1, 'CG': 2, 'CLG':

我正在使用CSV文件作为输入,开发一个自定义elo/团队评级计算器。在Excel中使用openpyxl时,我可以得到类似的逻辑,但现在我正尝试在pandas中实现它,以便更好地与jupyter和matplotlib集成。但是,我在数据帧中的单个单元格上运行计算时遇到问题

def find_team_row(team_name):
    switcher = {
        '100T': 0,
        'C9': 1,
        'CG': 2,
        'CLG': 3,
        'FOX': 4,
        'FLY': 5,
        'GGS': 6,
        'OPT': 7,
        'TL': 8,
        'TSM': 9,
    }
    return switcher.get(team_name, None)

def update_df():
    for column in range(1, df.columns.get_loc(df.columns[-1]), 3):
        for row in range(0,9):

            init_rating = df.iloc[row,column]
            opponent_name = df.iloc[row,column+1]
            match_result = df.iloc[row,column+2]
            oppo_rating = df.iloc[find_team_row(opponent_name),column]
针对此代码块引发以下异常:

ajisaksonmac:elo_calc ajisakson$ /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 "/Users/ajisakson/Google Drive/swe_projects/elo_calc/test2.py"
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/indexing.py", line 235, in _has_valid_tuple
    self._validate_key(k, i)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/indexing.py", line 2035, in _validate_key
    "a [{types}]".format(types=self._valid_types)
ValueError: Can only index by location with a [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/ajisakson/Google Drive/swe_projects/elo_calc/test2.py", line 74, in <module>
    update_df()
  File "/Users/ajisakson/Google Drive/swe_projects/elo_calc/test2.py", line 27, in update_df
    oppo_rating = df.iloc[find_team_row(opponent_name),column] 
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/indexing.py", line 1418, in __getitem__
    return self._getitem_tuple(key)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/indexing.py", line 2092, in _getitem_tuple
    self._has_valid_tuple(tup)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/indexing.py", line 239, in _has_valid_tuple
    "[{types}] types".format(types=self._valid_types)
ValueError: Location based indexing can only have [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array] types
ajisaksonmac:elo_calc ajisakson$/Library/Frameworks/Python.framework/Versions/3.7/bin/python3”/Users/ajisakson/Google Drive/swe_projects/elo_calc/test2.py”
回溯(最近一次呼叫最后一次):
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/pandas/core/index.py”,第235行,在_has_valid_tuple中
自我验证键(k,i)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/pandas/core/index.py”,第2035行,在_validate_key中
“a[{types}].”格式(types=self.\u有效类型)
ValueError:只能使用[integer,integer切片(包括起点,不包括终点)、整型列表、布尔数组]按位置索引
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“/Users/ajisakson/googledrive/swe_projects/elo_calc/test2.py”,第74行,在
更新_df()
文件“/Users/ajisakson/Google Drive/swe_projects/elo_calc/test2.py”,第27行,在update_df中
oppo_rating=df.iloc[查找团队行(对手名称),列]
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/pandas/core/index.py”,第1418行,在__
返回self.\u getitem\u元组(键)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/pandas/core/index.py”,第2092行,在_getitem_元组中
self.\u具有有效的\u元组(tup)
文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site packages/pandas/core/indexing.py”,第239行,在\u has\u有效的\u元组中
“[{types}]types.”格式(types=self.\u有效类型)
ValueError:基于位置的索引只能有[integer,integer切片(包括起点,不包括终点),整型列表,布尔数组]类型
因此,我尝试使用iloc访问数据帧中的单个单元格,但在分配oppo_评级的地方,我收到了这个值错误。我尝试了许多不同的方法将两个iloc参数都转换为整数,包括int()、.iat()、.at()、.loc()等。我不断收到错误,提示我的一个参数不是整数

以下是我试图操纵/计算的数据框架的第一部分:

请为df添加一个示例这是我在df帖子底部的链接。如果需要,我可以包括更多