Reputation: 167
I have a dataset :
df = pd.DataFrame({
'region' : ['a', 'a', 'a','b','b','b','c','c','c','c','d','d','d','f','f','f','f','g','g','g','g','g'],
'month' : [8,9,10,12,1,2,8,9,10,11,10,11,12,1,2,3,4,8,9,10,11,12],
'incident_count' : [5,7,3, 34,23,29,5,2,5,4,12,21,15,34,41,35,14, 25,32,43,21,19]})
I performed the following operations:
df = df.groupby(['region', 'month']).incident_count.sum().reset_index()
calculated the slope per region :
from scipy.stats import linregress
df1 = df.groupby('region').apply(lambda v: linregress(v.month, v.incident_count)[0]).reset_index()
Now I want to have these codes on one line of code. How can i do that?
Upvotes: 0
Views: 49
Reputation: 979
I am not too sure why you would want to write a one-liner since it would look huge.
The obvious answer would be just to pipe the two lines you wrote
df.groupby(['region', 'month']).incident_count.sum().reset_index().groupby('region').apply(lambda v: linregress(v.month, v.incident_count)[0]).reset_index()
You could give us more info on what exactly you are trying to do
Upvotes: 1