追加字符串并添加到dataframe列时出现“ufunc“add”未包含签名匹配类型的循环”错误

2024-05-02 23:48:00 发布

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

我想附加一些字符串并将其添加到dataframe中的新列中。第一个被截取的代码可以工作,当我尝试第二个代码时,它失败,出现以下错误:

numpy.core._exceptions.UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U21'), dtype('<U21')) -> dtype('<U21')

唯一的区别是第二个代码段包含另一个字符串_

是否有人对发生此错误的原因提出建议

第一个代码被截断:

df["identifier"]=df.index.get_level_values(0).values.astype(str) + df["mother tongue iso636-3"].astype(str)+ '_' + df["country iso3166-2"].astype(str)

第二个代码被截断:

df["identifier"]=df.index.get_level_values(0).values.astype(str) + '_' + df["mother tongue iso636-3"].astype(str)+ '_' + df["country iso3166-2"].astype(str)

Tags: 字符串代码dfgetindex错误levelvalues
3条回答

在添加字符串浇铸系列时,我也遇到了类似的错误

此lambda解决方案将稍微慢一点,但将确保添加的值为STR类型

df['level_values'] = df.index.get_level_values(0).values
df["identifier"] = df.apply(lambda x: \
    str(x['level_values']) + '_' + str(x["mother tongue iso636-3"]) \
    + '_' + str(df["country iso3166-2"]),axis=1)
df.drop('level_values',inplace=True)

出现异常的问题是索引函数

df.index.get_level_values(0).values.astype(str)

如果首先将其作为列添加到数据帧,然后使用列而不是函数,则问题将不再发生:

df['index'] = df.index.get_level_values(0).values
df["identifier"]=df['index'].astype(str) + '_' + df["mother tongue iso636-3"].astype(str)+ '_' + df["country iso3166-2"].astype(str)

我很惊讶你的第一个案子成功了。但如果您提供一个简单版本的数据框架,可能会有所帮助

既然你没有那样做,我就得编一个:(

In [321]: df = pd.DataFrame([[1,'foo'],[2,'bar']])                                                     
In [322]: df                                                                                           
Out[322]: 
   0    1
0  1  foo
1  2  bar

首先看一下index

In [323]: df.index.values                                                                              
Out[323]: array([0, 1])            # numeric in my case
In [324]: df.index.values.astype(str)                                                                  
Out[324]: array(['0', '1'], dtype='<U21')    # numpy dtype
In [325]: df.index.values.astype(str)+'_'                                                              
                                     -
UFuncTypeError                            Traceback (most recent call last)
<ipython-input-325-230387b2895a> in <module>
  > 1 df.index.values.astype(str)+'_'

UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U21'), dtype('<U21')) -> dtype('<U21')

未为numpy字符串定义+/add

现在查看字符串列:

In [330]: df[1].values                                                                                 
Out[330]: array(['foo', 'bar'], dtype=object)  # pandas uses python strings

将该数组转换为numpy str会产生相同的错误:

In [331]: df[1].values.astype(str)                                                                     
Out[331]: array(['foo', 'bar'], dtype='<U3')
In [332]: df.index.values.astype(str)+df[1].values.astype(str)                                         
                                     -
UFuncTypeError                            Traceback (most recent call last)
<ipython-input-332-7bc2436a1bf8> in <module>
  > 1 df.index.values.astype(str)+df[1].values.astype(str)

UFuncTypeError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U21'), dtype('<U21')) -> dtype('<U21')

这就是为什么我想知道为什么你的第一个案子会成功

如果我将对象数据类型字符串保持原样:

In [333]: df.index.values.astype(str)+df[1].values                                                     
Out[333]: array(['0foo', '1bar'], dtype=object)

numpyindex数组转换为对象数据类型(公共数据类型),并逐元素执行+,对于python字符串来说,这是串联

把这个想法用一个“u”来表示:

In [334]: df.index.values.astype(str).astype(object)+'_'+df[1].values                                  
Out[334]: array(['0_foo', '1_bar'], dtype=object)

相关问题 更多 >