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

  • Home
  • SEARCH
  • 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 8506311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:33:36+00:00 2026-06-11T02:33:36+00:00

Basically, I have a bunch of data where the first column is a string

  • 0

Basically, I have a bunch of data where the first column is a string (label) and the remaining columns are numeric values. I run the following:

data = numpy.genfromtxt('data.txt', delimiter = ',')

This reads most of the data well, but the label column just gets ‘nan’. How can I deal with this?

  • 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-11T02:33:38+00:00Added an answer on June 11, 2026 at 2:33 am

    By default, np.genfromtxt uses dtype=float: that’s why you string columns are converted to NaNs because, after all, they’re Not A Number…

    You can ask np.genfromtxt to try to guess the actual type of your columns by using dtype=None:

    >>> from StringIO import StringIO
    >>> test = "a,1,2\nb,3,4"
    >>> a = np.genfromtxt(StringIO(test), delimiter=",", dtype=None)
    >>> print a
    array([('a',1,2),('b',3,4)], dtype=[('f0', '|S1'),('f1', '<i8'),('f2', '<i8')])
    

    You can access the columns by using their name, like a['f0']…

    Using dtype=None is a good trick if you don’t know what your columns should be. If you already know what type they should have, you can give an explicit dtype. For example, in our test, we know that the first column is a string, the second an int, and we want the third to be a float. We would then use

    >>> np.genfromtxt(StringIO(test), delimiter=",", dtype=("|S10", int, float))
    array([('a', 1, 2.0), ('b', 3, 4.0)], 
          dtype=[('f0', '|S10'), ('f1', '<i8'), ('f2', '<f8')])
    

    Using an explicit dtype is much more efficient than using dtype=None and is the recommended way.

    In both cases (dtype=None or explicit, non-homogeneous dtype), you end up with a structured array.

    [Note: With dtype=None, the input is parsed a second time and the type of each column is updated to match the larger type possible: first we try a bool, then an int, then a float, then a complex, then we keep a string if all else fails. The implementation is rather clunky, actually. There had been some attempts to make the type guessing more efficient (using regexp), but nothing that stuck so far]

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

Sidebar

Related Questions

I basically have the following string in the format: A,B,C:D,E,F What I am trying
Basically I have a getjson call to call for a bunch load of data.
Basically I have a bunch of repeated data in a bunch of text files
Basically I have a bunch of submission forms that gather data in a generic
I'm getting confused by pointers in objective-c. Basically I have a bunch of static
So basically, I have a bunch of TextBoxes that the user gets to fill
I have a page that will basically be used to concatenate a bunch of
have one time consuming step that flattens a bunch of files. basically i'd like
I have a bunch of data that I'm pulling into my application which frankly
I have some classes that need a bunch of data tables to do their

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.