5 ms·
Here's a better example, in dplyr and pandas. I find the pandas very awkward in both operations, and dplyr very nice. Get the last n characters from a column s
by j88439h84 9y ago
Here's a better example, in dplyr and pandas. I find the pandas very awkward in both operations, and dplyr very nice.
Get the last n characters from a column string, where n is determined by another column. Then filter to rows where those characters are 'AB'.
(
df
.assign(last_n_chars=df.apply(lambda x: x['name'][-x['n_chars']:], axis=1))
.query("last_n_chars == 'AB'")
)
df %>%
mutate(last_n_chars=str_sub(name, start=-n_chars)) %>%
filter(last_n_chars == 'AB')