Make Several Operations In A Dataframe At Once
I am trying to do in a smart way several calculation by using .groupby with pandas dataframe, with the following data: import numpy as np import pandas as pd df = pd.DataFrame({'A
Solution 1:
You can use agg()
:
df.groupby(['A','B']).agg({'C': diff, 'D': [per, ratio1]})
To skip the renaming part you can call your functions var1
, var2
and var3
and use it in groupby
.
var1 = lambda x: max(x)-min(x)
var2 = lambda x: (max(x)-min(x))/max(x)
var3 = lambda x: (max(x)-min(x))/ len(x)
df.groupby(['A','B']).agg({'C': var1, 'D': [var2, var3]})
df.columns = df.columns.droplevel()
EDIT
Try with:
defvar1(x): returnmax(x)-min(x)
defvar2(x): return (max(x)-min(x))/max(x)
defvar3(x): return (max(x)-min(x))/ len(x)
EDIT of EDIT
This works for me on pandas
version 0.19.2
:
import numpy as np
import pandas as pd
df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
'foo', 'bar', 'foo', 'foo'],
'B' : ['one', 'one', 'two', 'three',
'two', 'two', 'one', 'three'],
'C' : np.random.randn(8),
'D' : np.random.randn(8)})
defvar1(x): returnmax(x)-min(x)
defvar2(x): return (max(x)-min(x))/max(x)
defvar3(x): return (max(x)-min(x))/ len(x)
df = df.groupby(['A','B']).agg({'C': var1, 'D': [var2, var3]})
df.columns = df.columns.droplevel()
Post a Comment for "Make Several Operations In A Dataframe At Once"