Subscribe to DSC Newsletter

I am looking for equivalent python code below R code. I learning how to do piping and mutate in python.

Any help is appreciated

library(dplyr)

iris %>% group_by(Species) %>%

summarise(Sepal.Length=sum(Sepal.Length),Sepal.Width=sum(Sepal.Width))%>%

mutate(ratio=Sepal.Length/Sepal.Width)

Views: 712

Reply to This

Replies to This Discussion

Don't really know the particulars of library(dplyr) but on the surface it looks like similar capabilities are provided by the Pandas package in Python (see: https://pandas.pydata.org).

Thanks for your time 

I got the answer here 

https://www.kaggle.com/questions-and-answers/60138#post350863

Dave King said:

Don't really know the particulars of library(dplyr) but on the surface it looks like similar capabilities are provided by the Pandas package in Python (see: https://pandas.pydata.org).

As an R user converting to Python for the model deployment benefits, I understand your pain. Numpy and Pandas are great, but I was very comfortable with the tidyverse packages in R and really miss dplyr. There is likely a better way, but this will get you there in one command. What was giving me trouble was how to refer to the aggregated sum from the groupby in the assign (lambda was the trick). Hope this helps

How I did this prior to figuring out the lambda trick to use the intermediary dataframe columns

Sorry, looks like you found your answer. It makes me feel better that the basic approach is the same.

@Mark Thanks a lot , 

Some one in the kaggle forum gave the same answer as you . You are right we have to assign to a new df to refer it later .

Thanks again ...

Mark E Nichols said:

Sorry, looks like you found your answer. It makes me feel better that the basic approach is the same.

I also found something called Dplython on github that might be interesting to look at.

Interesting, will take a look at it , Have you used it ? is it as good as dplyr ?

Thanks for your time 

James Theobald said:

I also found something called Dplython on github that might be interesting to look at.

I have not used it.

Prabhakaran Arivalagan said:

Interesting, will take a look at it , Have you used it ? is it as good as dplyr ?

Thanks for your time 

James Theobald said:

I also found something called Dplython on github that might be interesting to look at.

RSS

Videos

  • Add Videos
  • View All

Follow Us

© 2018   Data Science Central ®   Powered by

Badges  |  Report an Issue  |  Privacy Policy  |  Terms of Service