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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:35:49+00:00 2026-05-26T05:35:49+00:00

I have some R code I need to port to python. However, R’s magic

  • 0

I have some R code I need to port to python. However, R’s magic data.frame and ddply are keeping me from finding a good way to do this in python.

Sample data (R):

x <- data.frame(d=c(1,1,1,2,2,2),c=c(rep(c('a','b','c'),2)),v=1:6)

Sample computation:

y <- ddply(x, 'd', transform, v2=(v-min(v))/(max(v)-min(v)))

Sample output:

  d c v  v2
1 1 a 1 0.0
2 1 b 2 0.5
3 1 c 3 1.0
4 2 a 4 0.0
5 2 b 5 0.5
6 2 c 6 1.0

So here’s my question for the pythonistas out there: how would you do the same? You have a data structure with a couple of important dimensions.

For each (c), and each(d) compute (v-min(v))/(max(v)-min(v))) and associate it with the corresponding (d,c) pair.

Feel free to use whatever data structures you want, so long as they’re quick on reasonably large datasets (those that fit in memory).

  • 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-26T05:35:50+00:00Added an answer on May 26, 2026 at 5:35 am

    Indeed pandas is the right (and only, I believe) tool for this in Python. It’s a bit less magical than plyr but here’s how to do this using the groupby functionality:

    df = DataFrame({'d' : [1.,1.,1.,2.,2.,2.],
                    'c' : np.tile(['a','b','c'], 2),
                    'v' : np.arange(1., 7.)})
    # in IPython
    In [34]: df
    Out[34]: 
       c  d  v
    0  a  1  1
    1  b  1  2
    2  c  1  3
    3  a  2  4
    4  b  2  5
    5  c  2  6
    

    Now write a small transform function:

    def f(group):
        v = group['v']
        group['v2'] = (v - v.min()) / (v.max() - v.min())
        return group
    

    Note that this also handles NAs since the v variable is a pandas Series object.

    Now group by the d column and apply f:

    In [36]: df.groupby('d').apply(f)
    Out[36]: 
       c  d  v  v2 
    0  a  1  1  0  
    1  b  1  2  0.5
    2  c  1  3  1  
    3  a  2  4  0  
    4  b  2  5  0.5
    5  c  2  6  1  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some really nice Python code to do what I need to do.
I have some Python code that uses Paramiko to grab build files from a
we have some C++ code that we need to create a make file in.
I have some code in my error handler I need to test against a
I have a console application that require to use some code that need administrator
I have an HTML textbox that contains some SQL code that I need executed.
I need to integrate some legacy 32-bit code - for which I don't have
I have some code which collects points (consed integers) from a loop which looks
I have this c code, where i need to calculate a dobule from a
I have some old python code that uses the pywin32 extensions. Starting out with

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.