【问题标题】:Python: ufunc 'add' did not contain a loop with signature matching types dtype('S21') dtype('S21') dtype('S21')Python:ufunc'add'不包含签名匹配类型dtype('S21')dtype('S21')dtype('S21')的循环
【发布时间】:2017-11-15 14:43:03
【问题描述】:

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

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

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

为了实现这一点,我打算创建一个键,它是 order_iddate 的串联,但是当我尝试以下代码时:

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 看起来像这样:

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)

【问题讨论】:

    标签: python pandas numpy dataframe concatenation


    【解决方案1】:

    问题是您不能将对象数组(包含字符串)添加到数字数组中,这只是模棱两可:

    >>> 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

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

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

    【讨论】:

    • df1['Date'].astype(str) 更惯用,与df1['Date'].apply(str)相比应该更快
    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 2021-11-23
    • 2021-07-28
    • 2016-08-06
    • 2018-07-25
    • 2019-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多