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

Clean up: Multi-parameter function lesson #86

Open
lwasser opened this issue Oct 28, 2024 · 0 comments
Open

Clean up: Multi-parameter function lesson #86

lwasser opened this issue Oct 28, 2024 · 0 comments
Labels
help wanted Extra attention is needed

Comments

@lwasser
Copy link
Member

lwasser commented Oct 28, 2024

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)
@lwasser lwasser added the help wanted Extra attention is needed label Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant