sum of columns #8815
Answered
by
gforsyth
ozgurkalan
asked this question in
Q&A
sum of columns
#8815
-
Hi there, my ibis table has several columns starting with FY, for fiscal years: FY1 9 to FY24 What could be the ibick way to handle this? |
Beta Was this translation helpful? Give feedback.
Answered by
gforsyth
Mar 28, 2024
Replies: 1 comment 1 reply
-
Hey @ozgurkalan -- I don't think there's a good way to do this right now. We're chatting on Zulip about what the API for this style of operation might look like. In the meantime, here's a slightly gross workaround that should do the trick: [ins] In [1]: from ibis.interactive import *
[ins] In [2]: t = ibis.memtable(
...: {
...: "fy20": [1, 2, 3, 4, 5],
...: "fy21": [5, 6, 7, 8.1, 9],
...: "fy22": [22, 22, 24, 25, 26],
...: }
...: )
[ins] In [3]: t
Out[3]:
┏━━━━━━━┳━━━━━━━━━┳━━━━━━━┓
┃ fy20 ┃ fy21 ┃ fy22 ┃
┡━━━━━━━╇━━━━━━━━━╇━━━━━━━┩
│ int64 │ float64 │ int64 │
├───────┼─────────┼───────┤
│ 1 │ 5.0 │ 22 │
│ 2 │ 6.0 │ 22 │
│ 3 │ 7.0 │ 24 │
│ 4 │ 8.1 │ 25 │
│ 5 │ 9.0 │ 26 │
└───────┴─────────┴───────┘
[ins] In [4]: t.mutate(
...: fy_total=sum(map(lambda x: getattr(t, x), t.select(s.startswith("fy")).columns))
...: )
Out[4]:
┏━━━━━━━┳━━━━━━━━━┳━━━━━━━┳━━━━━━━━━━┓
┃ fy20 ┃ fy21 ┃ fy22 ┃ fy_total ┃
┡━━━━━━━╇━━━━━━━━━╇━━━━━━━╇━━━━━━━━━━┩
│ int64 │ float64 │ int64 │ float64 │
├───────┼─────────┼───────┼──────────┤
│ 1 │ 5.0 │ 22 │ 28.0 │
│ 2 │ 6.0 │ 22 │ 30.0 │
│ 3 │ 7.0 │ 24 │ 34.0 │
│ 4 │ 8.1 │ 25 │ 37.1 │
│ 5 │ 9.0 │ 26 │ 40.0 │
└───────┴─────────┴───────┴──────────┘ |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
ozgurkalan
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @ozgurkalan -- I don't think there's a good way to do this right now. We're chatting on Zulip about what the API for this style of operation might look like. In the meantime, here's a slightly gross workaround that should do the trick: