【问题标题】:Python type error (dtype('<U51')Python 类型错误 (dtype('<U51')
【发布时间】:2018-11-30 19:51:26
【问题描述】:

我有一个数据框,其最终输出是按特定顺序连接的单元格。

目前我的代码如下

debit_creditquestionlist = ('Why is there a ' + df_credit[debit_credit] + ' in ' + df_credit[entity_name] + ' relating to ' + df_credit[account_name] +  ' of ' + df_credit[amount] + ' at ')

目前一切正常,除了df_credit[account_name],这是导致以下错误的原因

TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U51') dtype('<U51') dtype('<U51')

我查看了 Stack Overflow,dtype('&lt;U51') 表示它是一个数字。

当我添加 str 时,它会返回每个数字,而不仅仅是我需要的单个数字。

请问有人能描述一下如何解决这个问题吗?

【问题讨论】:

    标签: python python-3.x pandas


    【解决方案1】:

    你可能想这样做:

    df_credit[account_name].apply(str)
    

    将向量的每个元素转换为字符串变量。否则:

     str(df_credit[account_name]) 
    

    实际上是将整个向量转换为字符串,您似乎已经获得了结果。

    A similar question

    【讨论】:

      猜你喜欢
      • 2017-02-05
      • 2013-05-01
      • 2020-08-29
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 2020-07-10
      • 2016-07-02
      • 2013-09-30
      相关资源
      最近更新 更多