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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:42:58+00:00 2026-06-06T14:42:58+00:00

Similar to this R question , I’d like to apply a function to each

  • 0

Similar to this R question, I’d like to apply a function to each item in a Series (or each row in a DataFrame) using Pandas, but want to use as an argument to this function the index or id of that row. As a trivial example, suppose one wants to create a list of tuples of the form [(index_i, value_i), …, (index_n, value_n)]. Using a simple Python for loop, I can do:

In [1] L = []
In [2] s = Series(['six', 'seven', 'six', 'seven', 'six'],
           index=['a', 'b', 'c', 'd', 'e'])
In [3] for i, item in enumerate(s):
           L.append((i,item))
In [4] L
Out[4] [(0, 'six'), (1, 'seven'), (2, 'six'), (3, 'seven'), (4, 'six')]

But there must be a more efficient way to do this? Perhaps something more Panda-ish like Series.apply? In reality, I’m not worried (in this case) about returning anything meaningful, but more for the efficiency of something like ‘apply’. Any ideas?

  • 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-06T14:42:59+00:00Added an answer on June 6, 2026 at 2:42 pm

    If you use the apply method with a function what happens is that every item in the Series will be mapped with such a function. E.g.

    >>> s.apply(enumerate)
    a    <enumerate object at 0x13cf910>
    b    <enumerate object at 0x13cf870>
    c    <enumerate object at 0x13cf820>
    d    <enumerate object at 0x13cf7d0>
    e    <enumerate object at 0x13ecdc0>
    

    What you want to do is simply to enumerate the series itself.

    >>> list(enumerate(s))
    [(0, 'six'), (1, 'seven'), (2, 'six'), (3, 'seven'), (4, 'six')]
    

    What if for example you wanted to sum the string of all the entities?

    >>> ",".join(s)
    'six,seven,six,seven,six'
    

    A more complex usage of apply would be this one:

    >>> from functools import partial
    >>> s.apply(partial(map, lambda x: x*2 ))
    a                ['ss', 'ii', 'xx']
    b    ['ss', 'ee', 'vv', 'ee', 'nn']
    c                ['ss', 'ii', 'xx']
    d    ['ss', 'ee', 'vv', 'ee', 'nn']
    e                ['ss', 'ii', 'xx']
    

    [Edit]

    Following the OP’s question for clarifications: Don’t confuse Series (1D) with DataFrames (2D) http://pandas.pydata.org/pandas-docs/stable/dsintro.html#dataframe – as I don’t really see how you can talk about rows. However you can include indices in your function by creating a new series (apply wont give you any information about the current index):

    >>> Series([s[x]+" my index is:  "+x for x in s.keys()], index=s.keys())
    a      six index  a
    b    seven index  b
    c      six index  c
    d    seven index  d
    e      six index  e
    

    Anyhow I would suggest that you switch to other data types to avoid huge memory leaks.

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

Sidebar

Related Questions

(This question is similar to this one , but it's for using XMLHttpRequest instead
Similar to this question . I would like to find all commented files. But
Similar to This Question using linq to SQL , but I don't want to
Similar to this question , but taking it a step further. I would like
Note: This is similar to this question but it is not the same. I
Similar to this question Compile to a stand-alone executable (.exe) in Visual Studio But
Similar to this question but with objects instead of pointers. If I have the
This question is similar to this other post , but I'm new to iPhone
This question is similar to this one , but hopefully it will pick up
This question is pretty much similar to this one , but for SQL Server

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.