Python - Iterate over a list of attributes

前端 未结 3 960
借酒劲吻你
借酒劲吻你 2020-12-11 11:01

I have a feature in my data set that is a pandas timestamp object. It has (among many others) the following attributes: year, hour, dayofweek, month.

I can create ne

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-11 11:36

    operator.attrgetter

    You can extract attributes in a loop:

    from operator import attrgetter
    
    for i in nomtimes:
        df[i] = df['timeStamp'].apply(attrgetter(i))
    

    Here's a complete example:

    df = pd.DataFrame({'timeStamp': ['2018-05-05 15:00', '2015-01-30 11:00']})
    df['timeStamp'] = pd.to_datetime(df['timeStamp'])
    
    nomtimes = ['year', 'hour', 'month', 'dayofweek']
    
    for i in nomtimes:
        df[i] = df['timeStamp'].apply(attrgetter(i))
    
    print(df)
    
                timeStamp  year  hour  month  dayofweek
    0 2018-05-05 15:00:00  2018    15      5          5
    1 2015-01-30 11:00:00  2015    11      1          4
    

    Your code will not work because you are attempting to pass a string rather than extracting an attribute by name. Yet this isn't what's happening: the syntax does not feed the string but tries to access i directly, as demonstrated in your first example.

    Getting rid of the for loop

    You might ask if there's any way to extract all attributes from a datetime object in one go rather than sequentially. The benefit of attrgetter is you can specify multiple attributes directly to avoid a for loop altogether:

    attributes = df['timeStamp'].apply(attrgetter(*nomtimes))
    df[nomtimes] = pd.DataFrame(attributes.values.tolist())
    

    Using dt accessor instead of apply

    But pd.Series.apply is just a thinly veiled loop. Often, it's not necessary. Borrowing @juanpa.arrivillaga's idea, you an access attributes directly via the pd.Series.dt accessor:

    attributes = pd.concat(attrgetter(*nomtimes)(df['timeStamp'].dt), axis=1, keys=nomtimes)
    df = df.join(attributes)
    

提交回复
热议问题