简体   繁体   English

使用 group by 将更改应用于整个 dataframe

[英]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.我正在尝试对 dataframe 应用更改,以获取仅通过使用 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']).所以我想要找到每个公司的最小日期值,以便我可以将数字 0 应用于多个列中的第一个值(在本例中为 df2['Research and Development Expense Lag'] 和 df2['Capital Expenditures Lag' ])。 Here is what I have so far, a groupby that returns those minimum date values for each company:这是我到目前为止所拥有的,一个返回每个公司的最小日期值的 groupby:

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:您可以获取这些行的索引值,然后将它们与.loc[]一起使用来更改这两列中的值:

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. .get_level_values(1) function 用于提取 MultiIndex 的第二级。 The first level will contain Ticker Symbol values.第一级将包含Ticker Symbol值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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