简体   繁体   中英

python function that allows you to name the columns (as a function parameter)

I couldn't find the right search terms for this question, so please apologize if this question has already been asked before.

Basically, I want to create a python function that allows you to name the columns (as a function parameter) that you will do certain kinds of analyses on.

For instance see below. Obviously this code doesn't work because 'yearattribute' is taken literally after the df. I'd appreciate your help!

def networkpairs2(df, Year):
    """
    An effort to generalize the networkpairs function by allowing you to choose the
    organization and actor parameter column names
    """
    totaldf = df
    yearattribute = '%s' %Year
    print yearattribute
    yearlist = list(np.unique(df.yearattribute))
    print yearlist
    return 

If I understand you, df[yearattribute].unique() should work. You can index into DataFrame columns like a dictionary.

Aside #1: totaldf = df only makes totaldf a new name for df , it doesn't make a copy, and you don't use it anyway.

Aside #2: you're not returning anything.

You can use getattr here:

yearlist = list(np.unique(getattr(df, yearattribute)))

getattr allows you to access an attribute via a string representation of its name.

Below is a demonstration:

>>> class Foo:
...     def __init__(self):
...         self.attr = 'value'
...
>>> foo = Foo()
>>> getattr(foo, 'attr')
'value'
>>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM