简体   繁体   中英

ufunc 'add' did not contain a loop with signature matching types dtype

Why do I get this error?

ufunc 'add' did not contain a loop with signature matching types dtype

The code:

cols = df.columns.tolist()
cols = np.array (cols)
cols2 = cols[:17] + cols[19:22] + cols [18]

Thanks!

There are 2 issues

  • operator + , when used on numpy array, is interpreted as numerical addition, not list concatenation, thus the error about the matching dtype. Instead of addition, you should use np.concatenate
  • cols[18] is not an array -- it is an element of the array. You cannot add a number and a array (if what you want to do is to append the element to the array)
cols2 = np.concatenate([cols[:17], cols[19:22], [cols[18]]])

or you can keep cols as a list (not converting it to numpy array and use list addition):

cols = df.columns.tolist()
cols2 = cols[:17] + cols[19:22] + [cols[18]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM