简体   繁体   中英

Applying changes to entire dataframe using group by

I am trying to apply changes to a dataframe for values only returned (to the best of my knowledge) by using groupby. So what I want is to find the minimum date values for each company so that I can apply the number 0 to first value in several columns (in this case df2['Research and Development Expense Lag'] and df2['Capital Expenditures Lag']). Here is what I have so far, a groupby that returns those minimum date values for each company:

df2.groupby('Ticker Symbol').apply(lambda d: \
            d[d['Data Date'] == d['Data Date'].min()])

You are on the right track. You can get the index values for those rows and then use them with .loc[] to change values in those two columns:

df2.loc[df2.groupby('Ticker Symbol').apply(
        lambda d: d[d['Data Date'] == d['Data Date'].min()]
    )
    .index
    .get_level_values(1),
    ['Research and Development Expense Lag', 'Capital Expenditures Lag']
] = 0

The .get_level_values(1) function serves to extract the second level of the MultiIndex. The first level will contain Ticker Symbol values.

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