Skip to content

Clean up: Multi-parameter function lesson #86

@lwasser

Description

@lwasser

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:

  • opens the data as publication JSON,
  • convert it to pandas df, and
  • then drops some columns.

The second parameter of the function could be the column names to drop.

An example of this function is here in this lesson.

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions