TypeError: only integer scalar arrays can be converted to a scalar index in Python

Dung Do Tien Aug 24 2021 181

Hello Guys. I'm a newbie in Python 3.8.2. I want to concat two arrays and I used concatenate() method of numpy package. See below:

# import numpy
import numpy

# Create array
lstColor1st = numpy.array(['Black', 'Blue', 'Green', 'Orange'])
lstColor2nd = numpy.array(['Red', 'Yellow'])

# Concatenate array lstColor1st & lstColor2nd 
combindArr = numpy.concatenate(lstColor1st, lstColor2nd)
print(combindArr)

But when I run it I get an exception TypeError: only integer scalar arrays can be converted to a scalar index.

Traceback (most recent call last):
  File "main.py", line 9, in <module>
    combindArr = numpy.concatenate(lstColor1st, lstColor2nd)
  File "<__array_function__ internals>", line 5, in concatenate
TypeError: only integer scalar arrays can be converted to a scalar index

How can I fix it?

Have 1 answer(s) found.
  • J

    Jide Aug 24 2021

    To solve this error you need to convert array 1 and array 2 in to tuple or list. See an example:

    # import numpy
    import numpy
    
    # Create array
    lstColor1st = numpy.array(['Black', 'Blue', 'Green', 'Orange'])
    lstColor2nd = numpy.array(['Red', 'Yellow'])
    
    # Concatenate array ar1 & ar2
    combindArr = numpy.concatenate((lstColor1st, lstColor2nd))
    print(combindArr)

    OR

    # import numpy
    import numpy
    
    # Create array
    lstColor1st = numpy.array(['Black', 'Blue', 'Green', 'Orange'])
    lstColor2nd = numpy.array(['Red', 'Yellow'])
    
    # Concatenate array ar1 & ar2
    combindArr = numpy.concatenate([lstColor1st, lstColor2nd])
    print(combindArr)

    #Output

    ['Black' 'Blue' 'Green' 'Orange' 'Red' 'Yellow']
    
Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close