I have two dataframes, which both have an Order ID
and a date
.
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)]
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']
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:
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)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…