'pandas equivalent to mutate accros
I would like to perform following operation in Pandas:
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()
Is there any nice equivalent to it? Or at least some really nice one-liner using apply
function in pandas?
Solution 1:[1]
You could try (not sure if this qualifies for you as "nice")
# 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)
Solution 2:[2]
You could unpack a dictionary comprehension into the keyword arguments of assign
(assign
is similar to dplyr::mutate
).
import pandas as pd
from statsmodels.datasets import get_rdataset
mtcars = get_rdataset('mtcars').data
(mtcars
.assign(**{col: mtcars[col].add(100).add('at' in col)
for col in mtcars.filter(regex='t$')})
.filter(regex='t$')
.head(3)
)
Output:
drat wt
Mazda RX4 104.90 102.620
Mazda RX4 Wag 104.90 102.875
Datsun 710 104.85 102.320
Solution 3:[3]
One option is a combination of filter
and transform
:
(mtcars
.filter(regex = "t$")
.transform(lambda df: np.where('at' in df.name, df + 101, df+100))
.head(3)
)
drat wt
0 104.90 102.620
1 104.90 102.875
2 104.85 102.320
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | |
Solution 3 | sammywemmy |