0

I have a pandas df where one column lists a particular func used to get the result in that line of the df.

It appears that Python changes the name of a func. if it is part of a list of functions. So Python takes the func name 'strategy0' and changes it to the less useful '<function strategy0 at 0x0000000009CCA488>' if it is part of a list of functions.

How can I either avoid Python changing the fct name alltogether or use Reg. Ex. to create a new df column 'stratname' with the proper fct. name (changing it back)?

This is my df as it looks now:

                                     Strategy Ticker         ROI
0  <function strategy0 at 0x0000000009CCA488>   nflx  142.976946
1  <function strategy0 at 0x0000000009CCA488>   lnkd   61.710992
2  <function strategy0 at 0x0000000009CCA488>    hsp    8.589611

This is how I would like it to look:

    Strategy Ticker         ROI
0  strategy0   nflx  142.976946
1  strategy0   lnkd   61.710992
2  strategy0    hsp    8.589611

This way the column info could be used directly as input for a new function.

A good friend of mine helped me to the below code. But he is not profficient in pandas, so we're kind of struggling with it.

def format(x):
    m = re.search(r'<function\s+(strategy\d+)\s+.*, x)
    return m.groups(1)

df_max_res['stratname'] = df_max_res['Strategy'].applymap(format)

This seems to look a lot like my problem, but apparantly Python3 handles this differently than P2. Return function name in python?

Community
  • 1
  • 1
Morten
  • 67
  • 1
  • 7
  • Python 2 and Python 3 handle this the exact same way. The issue is that you're not printing the name; you're printing the function, and printing a function prints a bunch of other not-the-name stuff. – user2357112 May 27 '15 at 20:14

2 Answers2

1

Another solution if you like map and list

strategies = list(map(lambda x: x.__name__, strategies))
0

Apparantly it accepts f.__name__ but not f.func_name in Python3.

So my function list needs to look like this:

strategies = [strategy0.__name__, strategy1.__name__, strategy2.__name__, strategy3.__name__]

Now it works and I do'nt need to use RegEx. Phew!

Morten
  • 67
  • 1
  • 7