pyOpenSci / lessons

A repo containing lessons used in pyOpenSci training.
https://www.pyopensci.org/lessons
BSD 3-Clause "New" or "Revised" License
3 stars 4 forks source link

Clean up: Multi-parameter function lesson #86

Open lwasser opened 4 weeks ago

lwasser commented 4 weeks ago

Right now, the multi-parameter function lesson also uses earthly. Similar to #85 it would be ideal to remove the complexity that Earthpy adds and to instead open a .json file in one of our data directories.

Screenshot 2024-10-27 at 6 16 27 PM

We can have them create a small function that:

import [json](https://docs.python.org/3/library/json.html#module-json)
from [pathlib](https://docs.python.org/3/library/pathlib.html#module-pathlib) import [Path](https://docs.python.org/3/library/pathlib.html#pathlib.Path)

import [pandas](https://pandas.pydata.org/docs/index.html#module-pandas) as pd

def load_clean_json(file_path, columns_to_keep):
    """
    Load JSON data from a file. Drop unnecessary columns and normalize
    to DataFrame.

    Parameters
    ----------
    file_path : Path
        Path to the JSON file.
    columns_to_keep : list
        List of columns to keep in the DataFrame.

    Returns
    -------
    dict
        Loaded JSON data.
    """

    with file_path.open("r") as json_file:
        json_data = [json.load](https://docs.python.org/3/library/json.html#json.load)(json_file)
    return [pd.json_normalize](https://pandas.pydata.org/docs/reference/api/pandas.json_normalize.html#pandas.json_normalize)(json_data)