Merging the two “replace” methods?

  Kiến thức lập trình

Is it possible to merge the two “replace” methods in the example below into one? The intention is for scalability.

import pandas as pd

custom_groups = {"odd": [1, 3, 5], "even": [2, 4]}

s = pd.Series([1, 2, 3, 4, 5])

s.replace(custom_groups["odd"], "odd").replace(custom_groups["even"], "even")

You could write a loop:

for key in custom_groups:
    s.replace(custom_groups[key], key)

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT