Skip to main content

Get dictionaries from other objects

Project description

yl

Get dictionaries from other objects

To install: pip install yl

Overview

The yl package provides a set of utilities to convert data structures into more usable dictionary formats, particularly focusing on data extraction and transformation from pandas DataFrames. It also includes functionalities to safely read JSON files and merge dictionaries with customizable strategies.

Features

DataFrame to Dictionary Conversion

  • keyval_df: Convert a DataFrame into a dictionary using one column for keys and another for values.
  • df_to_dict_list: Transform a DataFrame into a list of dictionaries, with options to specify which columns to use for keys and values.
  • df_to_keyed_dict: Convert a DataFrame into a dictionary, using one column as keys and applying an optional transformation function to the rest of the data in each row.

JSON Utilities

  • json_str_file: Load and parse a JSON file into a Python object.
  • safe_json_str_file: Safely read a JSON file, ensuring the file is properly closed after reading.

Dictionary Utilities

  • filter_dicts_by_keys: Filter a list of dictionaries, retaining only specified keys.
  • merge_dicts: Merge multiple dictionaries into one, with a customizable strategy for handling key collisions.

Usage Examples

Convert DataFrame to Dictionary

import pandas as pd
df = pd.DataFrame({
    'id': [1, 2, 3],
    'name': ['Alice', 'Bob', 'Charlie'],
    'age': [25, 30, 35]
})
result = df_to_keyed_dict(df, 'id')
print(result)
# Output: {1: {'name': 'Alice', 'age': 25}, 2: {'name': 'Bob', 'age': 30}, 3: {'name': 'Charlie', 'age': 35}}

Read JSON File Safely

json_data = safe_json_str_file('example.json')
print(json_data)
# Assuming 'example.json' contains '{"key": "value"}'
# Output: {'key': 'value'}

Filter List of Dictionaries

dict_list = [{'name': 'Alice', 'age': 25, 'city': 'New York'}, {'name': 'Bob', 'age': 30, 'city': 'Chicago'}]
filtered = filter_dicts_by_keys(dict_list, ['name', 'city'])
print(filtered)
# Output: [{'name': 'Alice', 'city': 'New York'}, {'name': 'Bob', 'city': 'Chicago'}]

Merge Dictionaries with Custom Strategy

d1 = {'name': 'Alice', 'age': 25}
d2 = {'name': 'Bob', 'city': 'New York'}
merged = merge_dicts(d1, d2, merge_strategy=lambda values: ', '.join(map(str, values)))
print(merged)
# Output: {'name': 'Alice, Bob', 'age': 25, 'city': 'New York'}

Documentation

Each function in the yl package is documented with Python docstrings, providing detailed descriptions of their purpose, parameters, return values, and examples. This documentation can be accessed using the help function in Python, e.g., help(df_to_keyed_dict).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

yl-0.0.6.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

yl-0.0.6-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file yl-0.0.6.tar.gz.

File metadata

  • Download URL: yl-0.0.6.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for yl-0.0.6.tar.gz
Algorithm Hash digest
SHA256 81db42332a669fa099752d9d41f3f0746053dc04d8345b973a4bb44850da4a75
MD5 b1a9694e781853217d64fa845e2c4aae
BLAKE2b-256 b1d0cb59bb75585d93e3be0614a5fa52c29f66a832032236f8414ee2066be76d

See more details on using hashes here.

File details

Details for the file yl-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: yl-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for yl-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 eddd2ba001d6982b25ad298e64b8fcbe1a59e5b6d8a36c4957f9b98cc858643c
MD5 f77ffa477c5ad2da9eb374923cf2d947
BLAKE2b-256 d3c3b9b7d1c37c40ee4010cb04e949de9982b4e2fd736dcdb0354833be7314bb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page