Pipes

Pipes facilitate chaining together operations that expect pandas data structures as their first argument. By using pipes, we can build up complex workflows without needing to write highly nested and hard to read code. In general, pipes let us turn something like f(g(h(df), 20), x=True)) into the following, making it much easier to read:

df.pipe(h)\ # first call h(df)  .pipe(g, 20)\ # call g on the result with positional arg 20  .pipe(f, x=True) # call f on the result with keyword arg x=True

Say we wanted to print the dimensions of a subset of the Facebook dataframe with some formatting, but after calculating the Z-scores for all the columns. We could define this function as follows:

>>> def get_info(df):... return '%d rows, %d columns ...

Get Hands-On Data Analysis with Pandas now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.