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)
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 Dates to 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With