Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using transform together with nth

I'm adding a column with transform with the following code:

df['new_date'] = df.groupby('account')['date'].transform('last')

This works fine, however it by default drops NaNs (as documented in existing bugs here, here and here), which I would like to keep. The devs suggest using nth(-1) instead. No problem!

However, I can't figure out how to use it with transform. The error message for

`df.groupby('a')['b'].transform('nth')`

is nth() missing 1 required positional argument: 'n', which seems tantalisingly to suggest that transform recognises the method, as long as I can figure out a way to pass the index to it. But none of

df.groupby('a')['b'].transform('nth(-1)')
df.groupby('a')['b'].transform('nth'(-1))
df.groupby('a')['b'].transform('nth')(-1)

work. Is there some way to do this?

like image 938
Josh Friedlander Avatar asked Aug 31 '26 23:08

Josh Friedlander


1 Answers

Here is possible use second argument for value passed to GroupBy.nth:

np.random.seed(2015)

df = pd.DataFrame({'account': ['foo', 'bar', 'baz'] * 3,
                   'val': np.random.choice([np.nan, 1],size=9)})
#print (df)

df['val1'] = df.groupby('account')['val'].transform('last')
df['val2'] = df.groupby('account')['val'].transform('nth', -1)
print (df)
  account  val  val1  val2
0     foo  NaN   1.0   1.0
1     bar  NaN   1.0   NaN
2     baz  NaN   NaN   NaN
3     foo  NaN   1.0   1.0
4     bar  1.0   1.0   NaN
5     baz  NaN   NaN   NaN
6     foo  1.0   1.0   1.0
7     bar  NaN   1.0   NaN
8     baz  NaN   NaN   NaN
like image 54
jezrael Avatar answered Sep 03 '26 14:09

jezrael