Python:ufunc 'add' 不包含签名匹配类型 dtype('S21') dtype('S21') dtype('S21') 的循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44527956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:06:54  来源:igfitidea点击:

Python: ufunc 'add' did not contain a loop with signature matching types dtype('S21') dtype('S21') dtype('S21')

pythonpandasnumpydataframeconcatenation

提问by jeangelj

I have two dataframes, which both have an Order IDand a date.

我有两个数据框,它们都有一个Order ID和一个date.

I wanted to add a flag into the first dataframe df1: if a record with the same order idand dateis in dataframe df2, then add a Y:

我想在第一个数据帧中添加一个标志df1:如果具有相同order iddate位于数据帧中的记录df2,则添加一个Y

[ df1['R'] = np.where(orders['key'].isin(df2['key']), 'Y', 0)]

To accomplish that, I was going to create a key, which would be the concatenation of the order_idand date, but when I try the following code:

为了实现这一点,我将创建一个键,它是order_idand的连接date,但是当我尝试以下代码时:

df1['key']=df1['Order_ID']+'_'+df1['Date']

I get this error

我收到这个错误

ufunc 'add' did not contain a loop with signature matching types dtype('S21') dtype('S21') dtype('S21')

df1 looks like this:

df1 看起来像这样:

Date | Order_ID | other data points ... 
201751 4395674  ...
201762 3487535  ...

These are the datatypes:

这些是数据类型:

df1.info()
RangeIndex: 157443 entries, 0 to 157442
Data columns (total 6 columns):
Order_ID                                 157429 non-null object
Date                                     157443 non-null int64
...
dtypes: float64(2), int64(2), object(2)
memory usage: 7.2+ MB

df1['Order_ID'].values
array(['782833030', '782834969', '782836416', ..., '783678018',
       '783679806', '783679874'], dtype=object)

回答by MSeifert

The problem is that you can't add an object array (containing strings) to a number array, that's just ambiguous:

问题是您不能将对象数组(包含字符串)添加到数字数组中,这很不明确:

>>> import pandas as pd

>>> pd.Series(['abc', 'def']) + pd.Series([1, 2])
TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U21') dtype('<U21') dtype('<U21')

You need to explicitly convert your Datesto str.

您需要明确地将您Datesstr.

I don't know how to do that efficiently in pandas but you can use:

我不知道如何在 Pandas 中有效地做到这一点,但您可以使用:

df1['key'] = df1['Order_ID'] + '_' + df1['Date'].apply(str)  # .apply(str) is new