与 R / R 库的比较#
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:
功能/灵活性:每个工具能做什么/不能做什么
性能:操作速度有多快。最好能提供具体数字/基准测试
易用性:哪个工具更容易/更难使用(您可能需要根据代码并排比较来判断)
该页面还为这些 R 包的用户提供一些翻译指南。
快速参考#
We’ll start off with a quick reference guide pairing some common R operations using dplyr with pandas equivalents.
查询、过滤、采样#
R |
pandas |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
排序#
R |
pandas |
|---|---|
|
|
|
|
转换#
R |
pandas |
|---|---|
|
|
|
|
|
|
分组和汇总#
R |
pandas |
|---|---|
|
|
|
|
|
|
|
|
Base R#
使用 R 的 |c|_ 进行切片#
R 可以轻松地按名称访问 data.frame 列
df <- data.frame(a=rnorm(5), b=rnorm(5), c=rnorm(5), d=rnorm(5), e=rnorm(5))
df[, c("a", "c", "e")]
或按整数位置访问
df <- data.frame(matrix(rnorm(1000), ncol=100))
df[, c(1:10, 25:30, 40, 50:100)]
在 pandas 中选择多个列名很简单
可以通过 iloc 索引器属性和 numpy.r_ 的组合来实现按整数位置选择多个不连续的列。
aggregate#
在 R 中,您可能需要将数据分割成子集并计算每个子集的平均值。使用名为 df 的 data.frame 并将其分割为组 by1 和 by2:
df <- data.frame(
v1 = c(1,3,5,7,8,3,5,NA,4,5,7,9),
v2 = c(11,33,55,77,88,33,55,NA,44,55,77,99),
by1 = c("red", "blue", 1, 2, NA, "big", 1, 2, "red", 1, NA, 12),
by2 = c("wet", "dry", 99, 95, NA, "damp", 95, 99, "red", 99, NA, NA))
aggregate(x=df[, c("v1", "v2")], by=list(mydf2$by1, mydf2$by2), FUN = mean)
groupby() 方法类似于 R 的 base aggregate 函数。
有关更多详细信息和示例,请参阅 the groupby documentation 。
match / %in%#
在 R 中选择数据的一种常用方法是使用 %in%,它使用 match 函数定义。 %in% 运算符用于返回一个逻辑向量,指示是否存在匹配项:
s <- 0:4
s %in% c(2,4)
isin() 方法类似于 R 的 %in% 运算符:
match 函数返回一个向量,其中包含其第一个参数在其第二个参数中的匹配位置:
s <- 0:4
match(s, c(2,4))
有关更多详细信息和示例,请参阅 the reshaping documentation 。
tapply#
tapply 类似于 aggregate,但数据可以存储在不规则数组中,因为子类的大小可能不规则。使用名为 baseball 的 data.frame,并根据数组 team 检索信息:
baseball <-
data.frame(team = gl(5, 5,
labels = paste("Team", LETTERS[1:5])),
player = sample(letters, 25),
batting.average = runif(25, .200, .400))
tapply(baseball$batting.average, baseball.example$team,
max)
在 pandas 中,我们可以使用 pivot_table() 方法来处理这个问题:
有关更多详细信息和示例,请参阅 the reshaping documentation 。
subset#
query() 方法类似于 R 的基础 subset 函数。在 R 中,你可能希望获取 data.frame 中某一列的值小于另一列值的行:
df <- data.frame(a=rnorm(10), b=rnorm(10))
subset(df, a <= b)
df[df$a <= df$b,] # note the comma
在 pandas 中,有几种方法可以执行子集选择。你可以使用 query() ,或者像索引/切片一样传递一个表达式,以及标准的布尔索引:
有关更多详细信息和示例,请参阅 the query documentation 。
with#
在 R 中,使用名为 df 的 data.frame 中的列 a 和 b 的表达式使用 with 进行评估如下:
df <- data.frame(a=rnorm(10), b=rnorm(10))
with(df, a + b)
df$a + df$b # same as the previous expression
在 pandas 中,使用 eval() 方法的等效表达式为:
在某些情况下,eval() 会比纯 Python 的评估快得多。有关更多详细信息和示例,请参阅 the eval documentation 。
plyr#
plyr 是一个 R 库,用于数据分析的 split-apply-combine 策略。其函数围绕 R 中的三个数据结构:a 表示 arrays,l 表示 lists,d 表示 data.frame。下表显示了这些数据结构在 Python 中如何映射。
R |
Python |
|---|---|
array |
list |
lists |
dictionary or list of objects |
data.frame |
dataframe |
ddply#
在 R 中,使用名为 df 的 data.frame 进行汇总 x 按 month 分组的表达式为:
require(plyr)
df <- data.frame(
x = runif(120, 1, 168),
y = runif(120, 7, 334),
z = runif(120, 1.7, 20.7),
month = rep(c(5,6,7,8),30),
week = sample(1:4, 120, TRUE)
)
ddply(df, .(month, week), summarize,
mean = round(mean(x), 2),
sd = round(sd(x), 2))
在 pandas 中,使用 groupby() 方法的等效表达式为:
有关更多详细信息和示例,请参阅 the groupby documentation 。
reshape / reshape2#
meltarray#
在 R 中,使用名为 a 的 3 维数组将其融化为 data.frame 的表达式为:
a <- array(c(1:23, NA), c(2,3,4))
data.frame(melt(a))
在 Python 中,由于 a 是一个列表,你可以简单地使用列表推导式。
meltlist#
在 R 中,使用名为 a 的列表将其融化为 data.frame 的表达式为:
a <- as.list(c(1:4, NA))
data.frame(melt(a))
在 Python 中,这个列表将是元组的列表,因此 DataFrame() 方法会将其转换为所需的 dataframe。
有关更多详细信息和示例,请参阅 the Into to Data Structures documentation 。
meltdf#
在 R 中,使用名为 cheese 的 data.frame 重塑该 data.frame 的表达式为:
cheese <- data.frame(
first = c('John', 'Mary'),
last = c('Doe', 'Bo'),
height = c(5.5, 6.0),
weight = c(130, 150)
)
melt(cheese, id=c("first", "last"))
在 Python 中,melt() 方法是 R 的等效方法:
有关更多详细信息和示例,请参阅 the reshaping documentation 。
cast#
在 R 中,acast 是一个使用名为 df 的 data.frame 将其转换为更高维度数组的表达式:
df <- data.frame(
x = runif(12, 1, 168),
y = runif(12, 7, 334),
z = runif(12, 1.7, 20.7),
month = rep(c(5,6,7),4),
week = rep(c(1,2), 6)
)
mdf <- melt(df, id=c("month", "week"))
acast(mdf, week ~ month ~ variable, mean)
在 Python 中,最好的方法是使用 pivot_table() :
类似地,对于 dcast,它使用名为 df 的 R data.frame 根据 Animal 和 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 R
with(df, tapply(Amount, list(Animal, FeedType), sum))
Python 可以通过两种方式来处理这个问题。首先,类似于上面使用 pivot_table() :
第二种方法是使用 groupby() 方法:
有关更多详细信息和示例,请参阅 the reshaping documentation 或 the groupby documentation 。
factor#
pandas 有一个用于分类数据的类型。
cut(c(1,2,3,4,5,6), 3)
factor(c(1,2,3,2,2,3))
在 pandas 中,这可以通过 pd.cut 和 astype("category") 来实现:
有关更多详细信息和示例,请参阅 categorical introduction 和 API documentation 。还有一个关于 differences to R’s factor 的文档。