-1

I have a function that takes in two dataframes and a string as shown below but I can't quite figure out how to create a new dataframe where I want the name of the dataframe to be the string that I input into the function concatenated with a 'venue_df' at the end.

def basic_pivot(df1,df2,str):
    if str=='first':
       str+'venue_df'=df1.pivot_table(index=['venue_id','max'],
                                    values=['match','name','continent','country'],
                                    aggfunc={'match':'nunique','name':'last',
                                             'continent':'first','country':'first'})
    else:
       None
 return str+'venue_df'
  • What do you expect this to do? The "variable" is not visible outside of your function, naming it dynamically has no effect. What value do you want returned after the ``None`` clause? It does not assign anything to the "variable" yet tries to return it. – MisterMiyagi May 09 '20 at 16:44
  • Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – MisterMiyagi May 09 '20 at 16:45
  • This is just part of the function - The thing is that I want to call that variable numerous times later in the same function. – Nathan Godfrey May 09 '20 at 16:46
  • But you can't know the name ahead of time, so you could only refer to it as ``str+'venue_df'`` (if that were valid). Why not just use a regular variable named ``venue_df``? On the other hand, if you know for sure that ``str=='first'`` then you can just name the variable ``first_venue_df``, no need for dynamic variable creation. – MisterMiyagi May 09 '20 at 16:48
  • Ah yh, I guess that is true actually. I was probably just overcomplicating it a little. The intial reason I wanted this because I thought it'd be easier later on in the function when I was joining four or five different pivot tables to be able to reference which pivot tables based on the value of the string but think just using a regular variable would be a much easier way. – Nathan Godfrey May 09 '20 at 16:58

1 Answers1

0

The only way is by making a dictionary:

dictionary = {}
if str=='first':
    dictionary[str] = df
return dictionary

from your code:

 def basic_pivot(df1,df2,str):
    dictionary={}
    if str=='first':
       dictionary['venue_df'] =df1.pivot_table(index=['venue_id','max'],
                                    values=['match','name','continent','country'],
                                    aggfunc={'match':'nunique','name':'last',
                                             'continent':'first','country':'first'})
    else:
       return None
    return dictionary


dictionary['venue_df'].head()
SakuraFreak
  • 311
  • 1
  • 11
  • Sorry, I'm not quite following what you're doing here or how that would work for my code. Can you show me what you mean in the context of my code. E.g. how would I make that pivot table's name equal to first_venue_df so I can call it by typing in first_venue_df. – Nathan Godfrey May 09 '20 at 16:27
  • I suggest learning how to code in python first... Or else will get harder and harder everytime you progress with pandas – SakuraFreak May 09 '20 at 16:35
  • Thanks but this isn't quite what I wanted. – Nathan Godfrey May 09 '20 at 16:54
  • Given that the value of ``str`` is well-known in the ``if`` block, there is no problem at all just using the "result" of ``str+'venue_df'`` as a variable name – namely just using ``first_venue_df = ...``. – MisterMiyagi May 09 '20 at 16:56
  • Yh, that is true in this case but later on in the function I'll have situations where I want to name dfs where the str value isn't known so I'll probably just do what you suggested before and keep the naming more general. – Nathan Godfrey May 09 '20 at 17:04
  • The answer is what you want. With dictionary you can make it dynamic. Just read the example in the answer. I am using the variable str to name a key in the dictionary. – SakuraFreak May 10 '20 at 15:32