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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:53:37+00:00 2026-06-08T13:53:37+00:00

If memory servies me, in R there is a data type called factor which

  • 0

If memory servies me, in R there is a data type called factor which when used within a DataFrame can be automatically unpacked into the necessary columns of a regression design matrix. For example, a factor containing True/False/Maybe values would be transformed into:

1 0 0
0 1 0
or
0 0 1

for the purpose of using lower level regression code. Is there a way to achieve something similar using the pandas library? I see that there is some regression support within Pandas, but since I have my own customised regression routines I am really interested in the construction of the design matrix (a 2d numpy array or matrix) from heterogeneous data with support for mapping back and fort between columns of the numpy object and the Pandas DataFrame from which it is derived.

Update: Here is an example of a data matrix with heterogeneous data of the sort I am thinking of (the example comes from the Pandas manual):

>>> df2 = DataFrame({'a' : ['one', 'one', 'two', 'three', 'two', 'one', 'six'],'b' : ['x', 'y', 'y', 'x', 'y', 'x', 'x'],'c' : np.random.randn(7)})
>>> df2
       a  b         c
0    one  x  0.000343
1    one  y -0.055651
2    two  y  0.249194
3  three  x -1.486462
4    two  y -0.406930
5    one  x -0.223973
6    six  x -0.189001
>>> 

The ‘a’ column should be converted into 4 floating point columns (in spite of the meaning, there are only four unique atoms), the ‘b’ column can be converted to a single floating point column, and the ‘c’ column should be an unmodified final column in the design matrix.

Thanks,

SetJmp

  • 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-08T13:53:39+00:00Added an answer on June 8, 2026 at 1:53 pm

    There is a new module called patsy that solves this problem. The quickstart linked below solves exactly the problem described above in a couple lines of code.

    • http://patsy.readthedocs.org/en/latest/overview.html

    • http://patsy.readthedocs.org/en/latest/quickstart.html

    Here is an example usage:

    import pandas
    import patsy
    
    dataFrame = pandas.io.parsers.read_csv("salary2.txt") 
    #salary2.txt is a re-formatted data set from the textbook
    #Introductory Econometrics: A Modern Approach
    #by Jeffrey Wooldridge
    y,X = patsy.dmatrices("sl ~ 1+sx+rk+yr+dg+yd",dataFrame)
    #X.design_info provides the meta data behind the X columns
    print X.design_info
    

    generates:

    > DesignInfo(['Intercept',
    >             'sx[T.male]',
    >             'rk[T.associate]',
    >             'rk[T.full]',
    >             'dg[T.masters]',
    >             'yr',
    >             'yd'],
    >            term_slices=OrderedDict([(Term([]), slice(0, 1, None)), (Term([EvalFactor('sx')]), slice(1, 2, None)),
    > (Term([EvalFactor('rk')]), slice(2, 4, None)),
    > (Term([EvalFactor('dg')]), slice(4, 5, None)),
    > (Term([EvalFactor('yr')]), slice(5, 6, None)),
    > (Term([EvalFactor('yd')]), slice(6, 7, None))]),
    >            builder=<patsy.build.DesignMatrixBuilder at 0x10f169510>)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a memory block that is divided into a series of location that
I would like to use output caching with WCF Data Services and although there's
Is there any way of keeping a result variable in memory so I don't
I've got a series of structs (audio data) which I need to hold onto
I have a memory address pool with 1024 addresses. There are 16 threads running
Is there any difference between real-time & in-memory DB, or its just same? Also,
I have a data set of books and authors, with a many-to-many relationship. There
Is there any way to ensure key material gets securely erased from the memory
I have to kill frequently few processes(services) on my developer machine to release memory
Memory leak problem - NSConcreteData // to set tip - photo in photo frame

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.