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 8732841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:30:59+00:00 2026-06-13T09:30:59+00:00

My matrix looks like this. [‘Hotel’, ‘ excellent’, ‘ very good’, ‘ average’, ‘

  • 0

My matrix looks like this.

 ['Hotel', ' "excellent"', ' "very good"', ' "average"', ' "poor"', ' "terrible"', ' "cheapest"', ' "rank"', ' "total reviews"']
 ['westin', ' 390', ' 291', ' 70', ' 43', ' 19', ' 215', ' 27', ' 813']
 ['ramada', ' 136', ' 67', ' 53', ' 30', ' 24', ' 149', ' 49', ' 310 ']
 ['sutton place', '489', ' 293', ' 106', ' 39', ' 20', ' 299', ' 24', ' 947']
 ['loden', ' 681', ' 134', ' 17', ' 5', ' 0', ' 199', ' 4', ' 837']
 ['hampton inn downtown', ' 241', ' 166', ' 26', ' 5', ' 1', ' 159', ' 21', ' 439']
 ['shangri la', ' 332', ' 45', ' 20', ' 8', ' 2', ' 325', ' 8', ' 407']
 ['residence inn marriott', ' 22', ' 15', ' 5', ' 0', ' 0', ' 179', ' 35', ' 42']
 ['pan pacific', ' 475', ' 262', ' 86', ' 29', ' 16', ' 249', ' 15', ' 868']
 ['sheraton wall center', ' 277', ' 346', ' 150', ' 80', ' 26', ' 249', ' 45', ' 879']
 ['westin bayshore', ' 390', ' 291', ' 70', ' 43', ' 19', ' 199', ' 813']

I want to remove the top row and the 0th column from this and create a new matrix.

How do I do this?

Normally in java or so Id use the following code:

 for (int y; y< matrix[x].length; y++)
     for(int x; x < matrix[Y].length; x++)
      {
        if(x == 0 || y == 0)
         {
           continue
          }
          else
           {
             new_matrix[x][y] = matrix[x][y];
           }


      }

Is there a way such as this in python to iterate and selectively copy elements?

Thanks


EDIT

Im also trying to convert each matrix element from a string to a float as I iterate over the matrix.

This my updated modified code based on the answer below.

A = []
f = open("csv_test.csv",'rt')
try:
    reader = csv.reader(f)
    for row in reader:
        A.append(row)
 finally:
     f.close()

 new_list = [row[1:] for row in A[1:]]
 l = np.array(new_list)
 l.astype(np.float32)
 print l

However Im getting an error

  --> l.astype(np.float32)
       print l


      ValueError: setting an array element with a sequence.
  • 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-06-13T09:31:00+00:00Added an answer on June 13, 2026 at 9:31 am

    The Basic Idea

    Here’s what I came up with:

    >>> import numpy as np
    >>> l = [['hotel','good','bad'],['hilton',1,2],['ramada',3,4]]
    >>> a = np.array(l) # convert to a numpy array to make multi-dimensional slicing possible
    >>> a
    array([['hotel', 'good', 'bad'],
           ['hilton', '1', '2'],
           ['ramada', '3', '4']], 
          dtype='|S4')
    >>> a[1:,1:] # exclude the first row and the first column
    array([['1', '2'],
           ['3', '4']], 
          dtype='|S4')
    >>> a[1:,1:].astype(np.float32) # convert to float
    array([[ 1.,  2.],
           [ 3.,  4.]], dtype=float32)
    

    You can pass your 2d list to the numpy array constructor, slice the 2d array to get rid of the first row and column and then use the astype method to convert everything to a float.

    All on one line, that’d be:

    >>> l = [['hotel','good','bad'],['hilton',1,2],['ramada',3,4]]
    >>> np.array(l)[1:,1:].astype(np.float32)
    array([[ 1.,  2.],
           [ 3.,  4.]], dtype=float32)
    

    The ValueError

    You’re getting a ValueError because you actually have a jagged array. Using the variable new_list from the code in your question you can prove this to yourself:

    >>> [len(x) for x in new_list]
    [9, 9, 9, 9, 9, 9, 9, 9, 9, 8]
    

    The last row is only of length 8, instead of 9, like all the others. Given a 2d jagged list, the numpy.array constructor will create a 1d numpy array with a dtype of object. The entries in that array are Python lists. The astype call is attempting to convert Python lists to float32, which is failing. I’m guessing this was just a case of human error. If you fix the missing entry, you should be good to go.

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

Sidebar

Related Questions

I have a vector, v3, that looks like this: v3 <- matrix(c(1,1,3,3,2,2, 2,3,1,2,3,1, 3,2,2,1,1,3),
I have a matrix called matrix that looks like this: charge convexhull_xCoor convexhull_yCoor id
I need to print out a matrix in haskell so it looks like this:
The initial square matrix looks like this: [,1] [,2] [,3] [,4] [,5] [,6] [1,]
Say I have a matrix that looks something like this: {{foobar, 77},{faabar, 81},{foobur, 22},{faabaa,
I have A matrix part of which looks like this : 4080530 92 --
I got a map matrix that looks like this: def matrix = [ field1:[role1:[state1:[f1r1s1,true],
I have a matrix (Data) which looks like this: (start) (stop) (strand) (gene number)
I have a matrix multiply code that looks like this: for(i = 0; i
I have a matrix that looks like this: SNP G1 G2 G3 marker1 TT

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.