Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7576453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:55:27+00:00 2026-05-30T16:55:27+00:00

I have a numpy array called landuse that’s a series of numbers 1-3 representing

  • 0

I have a numpy array called “landuse” that’s a series of numbers 1-3 representing different landuse categories. I want to convert this to a string based on a lookup table.

ids = [0,1,2,3]
lookup_table = ['None', 'Forest', 'Water', 'Urban']
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:55:28+00:00Added an answer on May 30, 2026 at 4:55 pm

    First let me explain why your loop isn’t working, in python an assignment, ie a = 1 takes the object 1 and gives it the name a. When you do name = "Water", name forgets what it was pointing to before and now points to "Water", but that doesn’t mean the previous object that was assigned to name gets replaced with "Water".

    That’s the problem, and now for a fix. If you have your landuse as an array of integer codes you can just use a lookup table. The table should be big enough so you don’t get an indexing error when you do lookup_table[landuse.max()]

    import numpy as np
    landuse = np.array([1,2,3,1,2,4])
    lookup_table = np.array(['None', 'Forest', 'Water', 'Urban', 'Other'])
    landuse_title = lookup_table[landuse]
    

    And for the final part of your question, the numpy ndarray is a homogenous data structure, meaning everything in the array must have the same data type. With that limitation in mind, it should be clear that you cannot take a row of the integers and replace it with a row of strings. Numpy does have “flexible dtypes” which allow you to do something like:

    >>> dt = np.dtype([('name', 'S4'), ('age', 'int'), ('height', 'float')])
    >>> array = np.array([('Mark', 25, 70.5),('Ben',40,72.75)], dtype=dt)
    >>> array
    array([('Mark', 25, 70.5), ('Ben', 40, 72.75)], 
          dtype=[('name', '|S4'), ('age', '<i4'), ('height', '<f8')])
    >>> array.shape
    (2,)
    >>> array['name']
    array(['Mark', 'Ben'], 
      dtype='|S4')
    

    We’ve created an array that hold a name, age and height for each person, but notice that the shape of the array is (2,) because we have two “people” in the array. I’m not sure exactly what your needs are, but you could try and use the flexible dtype to hold all the information in one array if that’s what you need. Depending on what my end goal, I often find it’s easier to just use a few separate arrays, or a list of arrays. Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a 2D numpy array that I want to plot in a colorbar.
I have a numpy array and I want to force every element that is
I have a numpy array of pixel data that I want to draw at
I have a numpy array and want to convert it into an ITK image
I have a NumPy array [1,2,3,4,5,6,7,8,9,10,11,12,13,14] and want to have an array structured like
I have a function foo that takes a NxM numpy array as an argument
I have an object of type 'numpy.ndarray', called myarray, that when printed to the
I have a Numpy array that looks like >>> a array([[ 3. , 2.
I have the following 3 x 3 x 3 numpy array called a (the
This is a simplification of my question. I have a numpy array: x =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.