Petr
Petr

Reputation: 1847

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?

Upvotes: 1

Views: 1196

Answers (3)

sammywemmy
sammywemmy

Reputation: 28709

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

Upvotes: 1

Levi Baguley
Levi Baguley

Reputation: 738

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

Upvotes: 1

Tino Kanngie&#223;er
Tino Kanngie&#223;er

Reputation: 136

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)

Upvotes: 1

Related Questions