Skip to content

Dynamically mutate a column #6936

Answered by jcrist
ozgurkalan asked this question in Q&A
Aug 25, 2023 · 2 comments · 1 reply
Discussion options

You must be logged in to vote

No need to drop and relabel the column. You can control the name of the mutated columns using either the .name method on a column expression, or by passing in the mutated columns via keywords as names.

In [1]: import ibis

In [2]: ibis.options.interactive = True

In [3]: from ibis import _

In [4]: t = ibis.memtable({"x": [1, 2, 3], "y": [-1, -2, -3]})

In [5]: t
Out[5]: 
┏━━━━━━━┳━━━━━━━┓
┃ xy     ┃
┡━━━━━━━╇━━━━━━━┩
│ int64int64 │
├───────┼───────┤
│     1-1 │
│     2-2 │
│     3-3 │
└───────┴───────┘

In [6]: col = "y"

In [7]: t.mutate(_[col].abs().name(col))  # mutate col, and rename expr back to col
Out[7]: 
┏━━━━━━━┳━━━━━━━┓
┃ xy     ┃
┡━━━━━━━╇━━…

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@cpcloud
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by ozgurkalan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants