### Install gluestick using pip Source: https://github.com/hotgluexyz/gluestick/blob/master/README.md This command installs the gluestick Python module using pip. Ensure you have Python and pip installed. ```shell pip install gluestick ``` -------------------------------- ### Parse JSON tuples to columns using json_tuple_to_cols in Python Source: https://github.com/hotgluexyz/gluestick/wiki/json_tuple_to_cols This example demonstrates how to use the json_tuple_to_cols function to parse JSON data within the 'Line.SalesItemLineDetail.ItemRef' and 'Line.SalesItemLineDetail.ItemAccountRef' columns. It specifies custom key-value mappings for the conversion. ```python # Specify lookup keys qb_lookup_keys = {'key_prop': 'name', 'value_prop': 'value'} # Explode these into new columns invoices = (invoices.pipe(gs.json_tuple_to_cols, 'Line.SalesItemLineDetail.ItemRef', col_config={'cols': {'key_prop': 'Item', 'value_prop': 'Item Id'}, 'look_up': qb_lookup_keys}) .pipe(gs.json_tuple_to_cols, 'Line.SalesItemLineDetail.ItemAccountRef', col_config={'cols': {'key_prop': 'Item Ref', 'value_prop': 'Item Ref Id'}, 'look_up': qb_lookup_keys})) invoices[['Id', 'Item', 'Item Id', 'Item Ref', 'Item Ref Id']].head() ``` -------------------------------- ### Use array_to_dict_reducer with explode_json_to_cols Source: https://github.com/hotgluexyz/gluestick/wiki/array_to_dict_reducer Example of using array_to_dict_reducer with the explode_json_to_cols function to process serialized JSON data in a DataFrame. It extracts specific key-value pairs from a JSON array stored in a column. ```python # Grab the string value of entries invoices = invoices.pipe(gs.explode_json_to_cols, 'CustomField', reducer=gs.array_to_dict_reducer('Name', 'StringValue')) invoices[['Id', 'CustomField.Crew #']].head() ``` -------------------------------- ### Define array_to_dict_reducer Source: https://github.com/hotgluexyz/gluestick/wiki/array_to_dict_reducer Defines the array_to_dict_reducer function, which takes key and value properties to create a dictionary from an array of JSON objects. ```python def array_to_dict_reducer(key_prop, value_prop): """ :param key_prop: property in dictionary for key :param value_prop: property in dictionary for value :return: a dictionary that has all the accumulated values """ ``` -------------------------------- ### Define explode_json_to_cols function Source: https://github.com/hotgluexyz/gluestick/wiki/explode_json_to_cols Defines the explode_json_to_cols function which takes a dataframe and a column name as input. It supports optional keyword arguments for customization, likely related to how the JSON array is processed. ```python explode_json_to_cols(df, column_name, **kwargs): """ :param df : the dataframe :param column_name: the column that has the JSON in it. :param reducer: a reducer that will convert the array of JSON into a panda series :return df - a new data frame with the JSON line expanded into columns and rows """ ``` -------------------------------- ### Python: Read CSV Folder with Converters and Index Columns Source: https://github.com/hotgluexyz/gluestick/wiki/read_csv_folder Reads CSV files from a specified folder, allowing for custom converters and index columns per entity. It returns a dictionary where keys are entity names and values are pandas DataFrames. This function is useful for processing Singer-tapped CSV data. ```python read_csv_folder(path, converters={}, index_cols={}) :param path: the folder directory :param converters: a dictionary with an array of converters that are passed to read_csv, the key of the dictionary is the name of the entity. :param index_cols: a dictionary with an array of index_cols, the key of the dictionary is the name of the entity. :return: a dict of pandas.DataFrames. the keys of which are the entity names ``` ```python entity_data = read_csv_folder(CSV_FOLDER_PATH, index_cols={'Invoice': 'DocNumber'}, converters={'Invoice': {'Line': ast.literal_eval, 'CustomField': ast.literal_eval, 'Categories': ast.literal_eval}}) df = entity_data['Account'] ``` -------------------------------- ### Rename DataFrame columns using a dictionary mapping (Python) Source: https://github.com/hotgluexyz/gluestick/wiki/rename This Python code snippet demonstrates how to rename multiple columns in a Pandas DataFrame using the `rename` method with a dictionary. It maps old column names to new, more readable names. This is useful for cleaning and standardizing data. ```python # Let's clean up the names of these columns invoices = input_df.pipe(lambda x: x.rename(columns={'CustomerRef__value': 'CustomerId', 'CustomerRef__name': 'Customer', 'MetaData__LastUpdatedTime': 'LastUpdated', 'MetaData__CreateTime': 'CreatedOn', 'CurrencyRef__name': 'Currency', 'CurrencyRef__value': 'CurrencyCode'})) invoices.head() ``` -------------------------------- ### Define explode_json_to_rows Function in Python Source: https://github.com/hotgluexyz/gluestick/wiki/explode_json_to_rows Defines the explode_json_to_rows function which takes a DataFrame, a column name containing JSON arrays, and an optional drop parameter. It processes the specified column to expand JSON objects into rows and columns. ```python def explode_json_to_rows(df, column_name, drop=True, **kwargs): """ :param df : the dataframe :param column_name: the column that has the JSON in it. :param drop: drop the source column in the result. Default is True df - a new data frame with the JSON line expanded into columns and rows """ ``` -------------------------------- ### Explode JSON array in DataFrame using gluestick Source: https://github.com/hotgluexyz/gluestick/wiki/explode_json_to_cols Demonstrates using the explode_json_to_cols function with a custom reducer to extract specific fields ('Name', 'StringValue') from a JSON array in the 'CustomField' column of a pandas DataFrame. The output is then displayed using head(). ```python invoices = invoices.pipe(gs.explode_json_to_cols, 'CustomField', reducer=gs.array_to_dict_reducer('Name', 'StringValue')) invoices[['Id', 'CustomField.Crew #']].head() ``` -------------------------------- ### Define json_tuple_to_cols function in Python Source: https://github.com/hotgluexyz/gluestick/wiki/json_tuple_to_cols This Python function takes a DataFrame, a column name containing JSON tuples, and an optional configuration for column mapping. It returns a modified DataFrame with the JSON tuples expanded into new columns. ```python def json_tuple_to_cols(df, column_name, col_config={'cols': {'key_prop': 'Name', 'value_prop': 'Value'}, 'look_up': {'key_prop': 'name', 'value_prop': 'value'}}): """ :param df: the data frame :param column_name: column with the json tuple :param col_config: conversion config :return: a modified dataframe """ ``` -------------------------------- ### Explode JSON Array Column in Pandas DataFrame Source: https://github.com/hotgluexyz/gluestick/wiki/explode_json_to_rows Demonstrates how to use the explode_json_to_rows function from the gluestick library to flatten a JSON column in a Pandas DataFrame. It specifies the column to process and a maximum nesting level for the JSON objects. ```python # Let's explode the Line column now invoices = invoices.pipe(gs.explode_json_to_rows, "Line", max_level=1) invoices.head() ``` -------------------------------- ### Define rename function for DataFrame column renaming (Python) Source: https://github.com/hotgluexyz/gluestick/wiki/rename This Python function renames specified columns in a Pandas DataFrame. It takes the DataFrame and a dictionary of target columns as input and returns the modified DataFrame. Ensure the input DataFrame and column mapping are correctly formatted. ```python def rename(df, target_columns): """ :param df : dataframe :param target_columns: the columns to rename as a dictionary :returns df: a modified data frame """ ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.