Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to mimic the pipe operator from R in Python?

See the example below. Is it possible to put each Python operation on a new line? If I have many operations, the code is not all visible. I need to scroll the VS Code window to the right. Thanks

# R code
data %>%
  group_by(sex) %>%
  summarise(avg_height=mean(height))

# Python code
data.groupby(['sex']).agg(avg_height=('height', 'mean')).reset_index()
like image 687
the_data_guy Avatar asked Nov 30 '25 12:11

the_data_guy


1 Answers

In Python, line breaks inside parentheses are ignored, so you could rewrite as:

(data
    .groupby(['sex'])
    .agg(avg_height=('height', 'mean'))
    .reset_index())

This post may be helpful.

like image 145
the_steve Avatar answered Dec 03 '25 00:12

the_steve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!