Pandas TypeError:只能将str(而不是“int”)连接到str

2024-10-04 11:28:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个问题,两个几乎相同的html文件在代码上创建了不同的行为。如果我在第一张图片中选择HTML文件,pandasGUI将很好地加载数据帧。但是,如果我选择第二个HTML文件,它会抛出一个typeerror,如标题中所述。我已经断断续续地尝试了三个星期,我完全迷失了方向。有人能帮忙吗?代码如下

import pandas as pd
from scipy.interpolate import interp1d
from pandasgui import show

pd.set_option('display.max_columns', 100)
pd.set_option('precision', 3)
file = ''

def choose_file():
    global file
    i = input("Please choose an option below: \n"
              "1: Own Team \n"
              "2: Shortlist \n")
    if (i == '1'):
        file = './own_team.html'
    elif (i == '2'):
        file = './shortlist.html'
    return file

file = choose_file()
attribute_view = pd.read_html(file)

map = interp1d([1, 7000], [1, 100])

df = attribute_view[0]

if(file == './shortlist.html'):
    for column in ['Inf', 'Name', 'Age', 'Best Pos', 'Personality', 'Acc', 'Wor', 'Vis', 'Thr', 'Tec', 'Tea', 'Tck', 'Str', 'Sta', 'TRO', 'Ref', 'Pun', 'Pos', 'Pen', 'Pas', 'Pac', '1v1', 'OtB', 'Nat', 'Mar', 'L Th', 'Lon', 'Ldr', 'Kic', 'Jum', 'Hea', 'Han', 'Fre', 'Fla', 'Fir', 'Fin', 'Ecc', 'Dri', 'Det', 'Dec', 'Cro', 'Cor', 'Cnt', 'Cmp', 'Com', 'Cmd', 'Bra', 'Bal', 'Ant', 'Agi', 'Agg', 'Aer']:
        df[column] = df[column].replace('-', 0)
    print(df)

df['Team_dna'] = (df['Agg'] + df['Ant'] + df['Det'] + df['Tea'] + df['Wor'] + df['Acc'] + df['Sta'])

# Sweeper Keeper - Attack
df['sk_at'] = map((df['Aer'] + df['Com'] + df['Fir'] + df['Han'] + df['Pas'] + df['Ref'] + df['TRO'] + df['Thr'] + df['Cmp'] + df['Dec'] + df['Vis'] + df['Acc']) * 40)

Working HTML

Not working HTML

编辑: 完全回溯:

Please choose an option below: 
1: Own Team 
2: Shortlist 
2
Traceback (most recent call last):
  File "C:\Users\Pottemuld\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\ops\array_ops.py", line 149, in na_arithmetic_op
    result = expressions.evaluate(op, str_rep, left, right)
  File "C:\Users\Pottemuld\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\computation\expressions.py", line 208, in evaluate
    return _evaluate(op, op_str, a, b)
  File "C:\Users\Pottemuld\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\computation\expressions.py", line 70, in _evaluate_standard
    return op(a, b)
TypeError: can only concatenate str (not "int") to str

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/Github/FM20-Analysis/FM.py", line 36, in <module>
    df['sk_at'] = map((df['Aer'] + df['Com'] + df['Fir'] + df['Han'] + df['Pas'] + df['Ref'] + df['TRO'] + df['Thr'] + df['Cmp'] + df['Dec'] + df['Vis'] + df['Acc']) * 40)
  File "C:\Users\Pottemuld\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\ops\common.py", line 64, in new_method
    return method(self, other)
  File "C:\Users\Pottemuld\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\ops\__init__.py", line 503, in wrapper
    result = arithmetic_op(lvalues, rvalues, op, str_rep)
  File "C:\Users\Pottemuld\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\ops\array_ops.py", line 197, in arithmetic_op
    res_values = na_arithmetic_op(lvalues, rvalues, op, str_rep)
  File "C:\Users\Pottemuld\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\ops\array_ops.py", line 151, in na_arithmetic_op
    result = masked_arith_op(left, right, op)
  File "C:\Users\Pottemuld\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\ops\array_ops.py", line 94, in masked_arith_op
    result[mask] = op(xrav[mask], yrav[mask])
TypeError: can only concatenate str (not "int") to str

Process finished with exit code 1

Tags: inpypandasdflocallineusersappdata