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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:31:10+00:00 2026-05-16T04:31:10+00:00

I need to take a csv file and import this data into a multi-dimensional

  • 0

I need to take a csv file and import this data into a multi-dimensional array in python, but I am not sure how to strip the ‘None’ values out of the array after I have appended my data to the empty array.

I first created a structure like this:

storecoeffs = numpy.empty((5,11), dtype='object')

This returns an 5 row by 11 column array populated by ‘None’.

Next, I opened my csv file and converted it to an array:

coeffsarray = list(csv.reader(open("file.csv")))

coeffsarray = numpy.array(coeffsarray, dtype='object')

Then, I appended the two arrays:

newmatrix = numpy.append(storecoeffs, coeffsarray, axis=1)

The result is an array populated by ‘None’ values followed by the data that I want (first two rows shown to give you an idea as to the nature of my data):

array([[None, None, None, None, None, None, None, None, None, None, None,
    workers, constant, hhsize, inc1, inc2, inc3, inc4, age1, age2,
    age3, age4],[None, None, None, None, None, None, None, None, None, None, None,
    w0, 7.334, -1.406, 2.823, 2.025, 0.5145, 0, -4.936, -5.054, -2.8, 0],,...]], dtype=object)

How do I remove those ‘None’ objects from each row so what I am left with is the 5 x11 multidimensional array with my data?

  • 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-16T04:31:11+00:00Added an answer on May 16, 2026 at 4:31 am

    @Gnibbler’s answer is technically correct, but there’s no reason to create the initial storecoeffs array in the first place. Just load in your values and then create an array from them. As @Mermoz noted, though, your use case looks simple enough for numpy.loadtxt().

    Beyond that, why are you using an object array?? It’s probably not what you want… Right now, you’re storing the numerical values as strings, not floats!

    You have essentially two ways to handle your data in numpy. If you want easy access to named columns, use a structured array (or a record array). If you want to have a “normal” multidimensional array, just use an array of floats, ints, etc. Object arrays have a specific purpose, but it’s probably not what you’re doing.

    For example:
    To just load in the data as a normal 2D numpy array (assuming all your data can be represented easily as a float):

    import numpy as np
    # Note that this ignores your column names, and attempts to 
    # convert all values to a float...
    data = np.loadtxt('input_filename.txt', delimiter=',', skiprows=1)
    
    # Access the first column 
    workers = data[:,0]
    

    To load your data in as a structured array, you might do something like this:

    import numpy as np
    infile = file('input_filename.txt')
    
    # Read in the names of the columns from the first row...
    names = infile.next().strip().split()
    
    # Make a dtype from these names...
    dtype = {'names':names, 'formats':len(names)*[np.float]}
    
    # Read the data in...
    data = np.loadtxt(infile, dtype=dtype, delimiter=',')
    
    # Note that data is now effectively 1-dimensional. To access a column,
    # index it by name
    workers = data['workers']
    
    # Note that this is now one-dimensional... You can't treat it like a 2D array
    data[1:10, 3:5] # <-- Raises an error!
    
    data[1:10][['inc1', 'inc2']] # <-- Effectively the same thing, but works..
    

    If you have non-numerical values in your data and want to handle them as strings, you’ll need to use a structured array, specify which fields you want to be strings, and set a max length for the strings in the field.

    From your sample data, it looks like the first column, “workers” is a non-numerical value that you might want to store as a string and all the rest look like floats. In that case, you’d do something like this:

    import numpy as np
    infile = file('input_filename.txt')
    names = infile.next().strip().split()
    
    # Create the dtype... The 'S10' indicates a string field with a length of 10
    dtype = {'names':names, 'formats':['S10'] + (len(names) - 1)*[np.float]}
    data = np.loadtxt(infile, dtype=dtype, delimiter=',')
    
    # The "workers" field is now a string array
    print data['workers']
    
    # Compare this to the other fields
    print data['constant']
    

    If there are cases where you really need the flexibility of the csv module (e.g. text fields with commas), you can use it to read the data, and then convert it to a structured array with the appropriate dtype.

    Hope that makes things a bit clearer…

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

Sidebar

Ask A Question

Stats

  • Questions 490k
  • Answers 490k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Simply pass by reference and then do isset check: function… May 16, 2026 at 9:54 am
  • Editorial Team
    Editorial Team added an answer In Access query builder's dialect of sql, you need ?… May 16, 2026 at 9:54 am
  • Editorial Team
    Editorial Team added an answer First, what's your goal? Why do you want to restrict… May 16, 2026 at 9:54 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I need to take a data.frame in the format of: id1 id2 mean start
I just downloaded this csv of infoboxes of wikipedia from dbpedia. However I have
I know I already have the Haskell Data.ByteString.Lazy function to split a CSV on
I often work in command line mysql. A common need is to take a
Here is the scenario: I get an CSV file, which has data1, data2, data3,
OK as you may know the CSV separator in French is not , it's
I need a way to take a block of HTML code and make all
I had a need for a method that could take a collection of strings,
I need an efficient data structure to store a list of integers. The amount
We ran into serious performance problems with our Oracle database and we would like

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.