Python 熊猫相当于突变的阿克罗斯
我想在熊猫中执行以下操作:Python 熊猫相当于突变的阿克罗斯,python,r,pandas,dplyr,Python,R,Pandas,Dplyr,我想在熊猫中执行以下操作: library(tidyverse) df <- tibble(mtcars) df %>% select(ends_with('t')) %>% head(3) # Accross all columns that ends with t, add value 100 hundred (+1 for true) if column contains 'at df %>% mutate(across(ends_wi
library(tidyverse)
df <- tibble(mtcars)
df %>%
select(ends_with('t')) %>%
head(3)
# Accross all columns that ends with t, add value 100 hundred (+1 for true) if column contains 'at
df %>%
mutate(across(ends_with('t'), ~ . + 100 + str_detect(cur_column(), 'at'))) %>%
select(ends_with('t') )%>%
head(3) %>% view()
有什么与之相当的好东西吗?或者至少有一些在pandas中使用apply函数的非常好的单行程序?您可以尝试不确定这是否适合您
# approach one
cols = [x for x in mtcars.columns if x.endswith("t")]
def f(x, cols):
for col in cols:
if "at" in col:
x[col] += 101
else:
x[col] += 100
return x
mtcars.apply(f, args=(cols,), axis=1)[cols].head(3)
# approach two
cols= [col for col in mtcars.columns if col.endswith("t")]
cols_w_at = [col for col in cols if "at" in col]
mtcars[cols] = mtcars[cols].apply(lambda x: x + 100)
mtcars[cols_w_at] = mtcars[cols_w_at].apply(lambda x: x + 1)
mtcars[cols].head(3)
您可以将字典理解解压到assign的关键字参数中,类似于dplyr::mutate 作为pd进口熊猫 从statsmodels.dataset导入getrdataset mtcars=获取数据集“mtcars”。数据 地铁车辆 .assign**{col:mtcars[col].add100.在col中添加'at' 用于mtcars.filterregex='t$'中的列 .filterregex='t$' .总目3 输出:
drat wt
Mazda RX4 104.90 102.620
Mazda RX4 Wag 104.90 102.875
Datsun 710 104.85 102.320