简体   繁体   English

在多索引 dataframe 上使用函数

[英]Using functions on multi-indexed dataframe

I think this is a basic question but I have not been able to find a usable solution yet.我认为这是一个基本问题,但我还没有找到可用的解决方案。 I have some data that is multi-index by month and year as in this attached figure我有一些按月和按年的多索引数据,如附图所示数据框 I want to do some transformations on some columns for each year and month .我想对yearmonth的某些列进行一些转换。 Let's say I have some function:假设我有一些 function:

def foo(series):
   return series/series.max()

So I would like to apply this function to some column (say, vol ) for every month of every year, rather than for all the data at once.所以我想将这个 function 应用到每年每个月的某个列(比如vol ),而不是一次应用所有数据。 Can someone help?有人可以帮忙吗?

You can use GroupBy.transform , because function return Series :您可以使用GroupBy.transform ,因为 function 返回Series

df['new'] = df.groupby(['year','month'])['vol'].transform(foo)

Alternative here is:这里的替代方案是:

df['new'] = df['vol'].div(df.groupby(['year','month'])['vol'].transform('max'))

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

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