Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boyd funksjon #28

Merged
merged 6 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/ssb_konjunk/data_formating.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""Funsjoner for å gjøre data formartering."""

import pandas as pd


def bytte_koder(
df: pd.DataFrame, kode_dict: dict[str, str], kolonnenavn: str
) -> pd.DataFrame:
"""Bytter koder.

Funksjonen for å bytte kode i en kolonne.


Args:
df: Pandas dataramme som vi skal sende inn.
kode_dict: Ordbok med gammel og ny kode.
kolonnenavn: Navn på kolonnen som skal byttes ut.

Returns:
Dataramme med ny kode.

"""
df_func = df.copy()

for old, new in kode_dict.items():

df_func.loc[df_func[kolonnenavn] == old, kolonnenavn] = str(new)

return df_func
22 changes: 22 additions & 0 deletions tests/test_data_formating.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import pandas as pd

from ssb_konjunk.data_formating import bytte_koder

"""Test av funksjon bytte_koder """


def test_bytte_koder() -> None:
kode_dict = {"a": "1", "b": "2"}

df = pd.DataFrame(
{
"bokstaver": ["a", "b"],
"tall": [1, 2],
}
)

df = bytte_koder(df, kode_dict, "bokstaver")

assert df["bokstaver"].to_list() == ["1", "2"]

assert df["tall"].to_list() == [1, 2]
Loading