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

后端 未结 1 1815
感动是毒
感动是毒 2020-12-06 09:43

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 reco

相关标签:
1条回答
  • 2020-12-06 09:51

    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
    
    0 讨论(0)
提交回复
热议问题