问题描述
我有两个数据帧,它们都有一个Order ID
和date
.
I have two dataframes, which both have an Order ID
and a date
.
我想在第一个数据帧df1
中添加一个标志:如果在数据帧df2
中有一个具有相同order id
和date
的记录,则添加一个Y
:
I wanted to add a flag into the first dataframe df1
: if a record with the same order id
and date
is in dataframe df2
, then add a Y
:
[ df1['R'] = np.where(orders['key'].isin(df2['key']), 'Y', 0)]
要做到这一点,我将创建一个键,该键将是order_id
和date
的串联,但是当我尝试以下代码时:
To accomplish that, I was going to create a key, which would be the concatenation of the order_id
and date
, but when I try the following code:
df1['key']=df1['Order_ID']+'_'+df1['Date']
我收到此错误
ufunc 'add' did not contain a loop with signature matching types dtype('S21') dtype('S21') dtype('S21')
df1看起来像这样:
df1 looks like this:
Date | Order_ID | other data points ...
201751 4395674 ...
201762 3487535 ...
这些是数据类型:
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)
推荐答案
问题是您无法将对象数组(包含字符串)添加到数字数组中,这是模棱两可的:
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')
您需要将Dates
明确转换为str
.
我不知道如何在熊猫中有效地做到这一点,但是您可以使用:
I don't know how to do that efficiently in pandas but you can use:
df1['key'] = df1['Order_ID'] + '_' + df1['Date'].apply(str) # .apply(str) is new
这篇关于Python:ufunc'add'不包含签名匹配类型为dtype('S21')dtype('S21')dtype('S21')的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!