Pipe() in pandas
pipe() method is used to chain a number of operations on a dataframe in a single compound statement. Its an effective way of reducing codelines and achieve a single long term objective from a data frame. (We may run pipes of series items too) In the given example we a doing the followiing things to achive the goal of getting the mean of the Points column in an extracted group from a dataframe.
- Creating the group based on year - df.groupby('Year')
- Filtering in a single group from the set of groups(get_group(2014))
- Getting the mean of column Points - x['Points'].agg('mean')
coding:
import pandas as pd
ipl_data = {'Team': ['Riders', 'Riders', 'Devils', 'Devils', 'Kings',
'kings', 'Kings', 'Kings', 'Riders', 'Royals', 'Royals', 'Riders'],
'Rank': [1, 2, 2, 3, 3,4 ,1 ,1,2 , 4,1,2],
'Year': [2014,2015,2014,2015,2014,2015,2016,2017,2016,2014,2015,2017],
'Points':[876,789,863,673,741,812,756,788,694,701,804,690]}
df = pd.DataFrame(ipl_data)
x = df.pipe(lambda x: df.groupby('Year')
.pipe(lambda x: x.get_group(2014))
.pipe(lambda x: x['Points'].agg('mean')))
x
output:
795.25
Another example of pipe
x = df.pipe(lambda x: df.groupby('Team')
.pipe(lambda x: x.get_group('kings'))
.pipe(lambda x: x['Points'].agg('sum')))
x
output:
812
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.