Skip to content

Instantly share code, notes, and snippets.

@khuyentran1401
Created January 5, 2022 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khuyentran1401/4d73d9a8cafcfce95e3b3ccbd8fe951f to your computer and use it in GitHub Desktop.
Save khuyentran1401/4d73d9a8cafcfce95e3b3ccbd8fe951f to your computer and use it in GitHub Desktop.
def str_to_interval(text: str):
if isinstance(text, str):
years = text.split()[0].split("-")
if len(years) == 2:
return pd.Interval(int(years[0]), int(years[1]))
else:
return pd.Interval(int(years[0]), float("inf"))
return text
# Convert string to pandas.Interval
normal_salary.YearsCoding = normal_salary.YearsCoding.apply(str_to_interval)
# Sort by years
years_sorted = normal_salary.dropna(
subset=["ConvertedSalary", "YearsCoding"]
).sort_values(by="YearsCoding")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment