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

Posted

技术标签:

【中文标题】Pandas TypeError:只能将str(不是“int”)连接到str【英文标题】:Pandas TypeError: can only concatenate str (not "int") to str 【发布时间】:2021-02-15 13:00:55 【问题描述】:

我有一个问题,两个几乎相同的 html 文件在代码上创建了不同的行为。如果我在第一张图片中选择 HTML 文件,pandasGUI 将很好地加载数据框。但是,如果我选择第二个 HTML 文件,它会抛出一个类型错误,如标题中所述。我已经尝试了三个星期来解决这个问题,但我完全迷失了。任何人都可以帮忙吗?代码如下。

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)

编辑: 完整追溯:

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

【问题讨论】:

哪一行代码报错了? 完整的回溯会更好地帮助确定问题所在 这是触发错误的行:df['sk_at'] = map((df['Aer'] + df['Com'] + df['Fir'] + df[ '汉'] + df['Pas'] + df['Ref'] + df['TRO'] + df['Thr'] + df['Cmp'] + df['Dec'] + df[' Vis'] + df['Acc']) * 40) 今天晚些时候可以提供完整的跟踪。 这似乎无法重现,但你说你已经花了好几天的时间。我会采用 df['sk_at']... 这一行,并在 map((...)*40) 内一次仅添加其中一列,然后查看哪一列将其中断。然后试着弄清楚为什么你的程序认为该列是一个字符串。 所以从 df['sk_at'] = map((df['Aer'])*40) 开始,看看你是否得到错误。然后 df['sk_at'] = map((df['Aer'] + df['Com'])*40)。看看你是否得到错误。等等等等。 【参考方案1】:

第二张图片在某些列中有值“-”。这会将整个列转换为字符串,您将无法运行算术运算。应该这样做!

df = df.replace('-', 0)

【讨论】:

谢谢,我回家试试,但确实有道理。 我仍然会使用它而不是遍历所有列

以上是关于Pandas TypeError:只能将str(不是“int”)连接到str的主要内容,如果未能解决你的问题,请参考以下文章

Pandas 合并错误 TypeError:“int”和“str”实例之间不支持“>”

TypeError:在使用 Python 进行网络抓取时,只能将 str(而不是“列表”)连接到 str 错误

解决 TypeError:只能将元组(不是“str”)连接到元组

TypeError:只能将列表(不是“str”)连接到列表 -

当 pandas 是导入时,Cx_freeze TypeError 只能连接列表(不是“NoneType”)以使用 numpy 依赖项列出

Pandas 错误“只能将 .str 访问器与字符串值一起使用”