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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file yl-0.0.5.tar.gz.
File metadata
- Download URL: yl-0.0.5.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d10d783b23e76c6eb13a74a79d552400b38eaf0f4aed80f55980eb22674a037
|
|
| MD5 |
c7e1cb96d29a6db6abf10c596c813863
|
|
| BLAKE2b-256 |
89bcf455dc31c2257393e4f627757ed3a9c05a559abbccd509b9086dd18a5eb7
|
File details
Details for the file yl-0.0.5-py3-none-any.whl.
File metadata
- Download URL: yl-0.0.5-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e1a86f461b04d1e4547783feaa1e7148732f851e61ad19aedf77fa3dc8082e3
|
|
| MD5 |
0bdac8d7634edcc76c5363449bb588d3
|
|
| BLAKE2b-256 |
def68a58728de209d61a4c66cc08945cb933a0906f08f39918ff5d983e5ba14f
|