Since pandas aims to provide a lot of the data manipulation and analysis
functionality that people use R for, this page
was started to provide a more detailed look at the R language and its many third
party libraries as they relate to pandas. In comparisons with R and CRAN
libraries, we care about the following things:
Functionality / flexibility: what can/cannot be done with each tool
Performance: how fast are operations. Hard numbers/benchmarks are
preferable
Ease-of-use: Is one tool easier/harder to use (you may have to be
the judge of this, given side-by-side code comparisons)
This page is also here to offer a bit of a translation guide for users of these
R packages.
Quick reference
We’ll start off with a quick reference guide pairing some common R
operations using dplyr with
pandas equivalents.
A common way to select data in R is using %in% which is defined using the
function match. The operator %in% is used to return a logical vector
indicating if there is a match or not:
tapply is similar to aggregate, but data can be in a ragged array,
since the subclass sizes are possibly irregular. Using a data.frame called
baseball, and retrieving information based on the array team:
The query() method is similar to the base R subset
function. In R you might want to get the rows of a data.frame where one
column’s values are less than another column’s values:
An expression using a data.frame called df in R with the columns a and
b would be evaluated using with like so:
df<-data.frame(a=rnorm(10),b=rnorm
plyr
plyr is an R library for the split-apply-combine strategy for data
analysis. The functions revolve around three data structures in R, a
for arrays, l for lists, and d for data.frame. The
table below shows how these data structures could be mapped in Python.
R
Python
array
list
lists
dictionary or list of objects
data.frame
dataframe
ddply
An expression using a data.frame called df in R where you want to
summarize x by month:
require(plyr)df<-data.frame(x=runif(120,1,168),y
reshape / reshape2
meltarray
An expression using a 3 dimensional array called a in R where you want to
melt it into a data.frame:
a<-array(c(1:23,NA),c(2,3,4))data.frame(melt(a))
In Python, since a is a list, you can simply use list comprehension.
In [28]: a=np.array(list(range(1,24))+[np.NAN]).reshape(2,3,4)In [29]: pd.DataFrame([tuple(list
meltlist
An expression using a list called a in R where you want to melt it
into a data.frame:
a<-as.list(c(1:4,NA))data.frame(melt(a))
In Python, this list would be a list of tuples, so
DataFrame() method would convert it to a dataframe as required.
meltdf
An expression using a data.frame called cheese in R where you want to
reshape the data.frame:
In pandas, there are a few ways to perform subsetting. You can use
query() or pass an expression as if it were an
index/slice as well as standard boolean indexing:
In [32]: cheese=pd.DataFrame( ....: { ....: "first":["John","Mary"], ....: "last":["Doe","Bo"], ....: "height":[5.5,6.0], ....: "weight":[130,150], ....: } ....: ) ....: In [33]: pd.melt(cheese,id_vars=["first","last"])Out[33]: first last variable value0 John Doe height 5.51 Mary Bo height 6.02 John Doe weight 130.03 Mary Bo weight 150.0In [34]: cheese.set_index(["first","last"]).stack(future_stack=True)# alternative wayOut[34]: first last John Doe height 5.5 weight 130.0Mary Bo height 6.0 weight 150.0dtype: float64
Similarly for dcast which uses a data.frame called df in R to
aggregate information based on Animal and FeedType:
df<-data.frame(Animal=c('Animal1','Animal2','Animal3','Animal2','Animal1','Animal2','Animal3'),FeedType=c('A','B','A','A','B','B','A'),Amount=c(10,7,4,2,5,6,2))dcast(df,Animal~FeedType,sum,fill=NaN)# Alternative method using base Rwith(df,tapply(Amount,list(Animal,FeedType),sum))
Python can approach this in two different ways. Firstly, similar to above
using pivot_table():
The second approach is to use the groupby() method:
In [40]: df.groupby(["Animal","FeedType"])["Amount"].sum()Out[40]: Animal FeedTypeAnimal1 A 10 B 5Animal2 A 2 B 13Animal3 A 6Name: Amount, dtype: int64