dtoolkit.accessor.dataframe.values_to_dict#

dtoolkit.accessor.dataframe.values_to_dict(df: DataFrame, /, order: list[Hashable] | Index = None, ascending: bool = True, unique: bool = True, to_list: bool = True, dropna: bool = True) dict[source]#

Convert values to dict.

Parameters:
orderlist of Hashable, Index, optional

The order of keys via given columns. If order is set, ascending will not work.

ascendingbool, default True

If True the key would use the few unique of column values first.

uniquebool, default True

If True would drop duplicate elements.

to_listbool, default True

If True one element value will return list.

dropnabool, default True

If True it will drop the nan value whatever it’s key or value.

Returns:
dict
Raises:
ValueError

If the columns of the inputting is not unique.

Notes

The same key of values would be merged into list.

Examples

>>> import json
>>> import dtoolkit
>>> import pandas as pd
>>> df = pd.DataFrame(
...     {
...         "x" : ["A", "A", "B", "B", "B"],
...         "y" : ["a", "b", "c", "d", "d"],
...         "z" : ["1", "2", "3", "3", "4"],
...     }
... )
>>> df
   x  y  z
0  A  a  1
1  A  b  2
2  B  c  3
3  B  d  3
4  B  d  4

Use few unique of column values as key first. The order of column unique values number is x < y < z. So the result will be {x: {y: [z]} }.

>>> print(json.dumps(df.values_to_dict(), indent=4))
{
    "A": {
        "a": [
            "1"
        ],
        "b": [
            "2"
        ]
    },
    "B": {
        "c": [
            "3"
        ],
        "d": [
            "3",
            "4"
        ]
    }
}

Use many unique of column values as key first, the result will be {y: {z: [x]} }.

>>> print(json.dumps(df.values_to_dict(ascending=False), indent=4))
{
    "a": {
        "1": [
            "A"
        ]
    },
    "b": {
        "2": [
            "A"
        ]
    },
    "c": {
        "3": [
            "B"
        ]
    },
    "d": {
        "3": [
            "B"
        ],
        "4": [
            "B"
        ]
    }
}

Output the arbitrary order like {z: x}  or ``{x: {z: [y]} }, via order argument.

>>> print(json.dumps(df.values_to_dict(order=["x", "z"]), indent=4))
{
    "A": [
        "1",
        "2"
    ],
    "B": [
        "3",
        "4"
    ]
}
>>> print(json.dumps(df.values_to_dict(order=["x", "z", "y"]), indent=4))
{
    "A": {
        "1": [
            "a"
        ],
        "2": [
            "b"
        ]
    },
    "B": {
        "3": [
            "c",
            "d"
        ],
        "4": [
            "d"
        ]
    }
}

It also could convert one column DataFrame. But ascending wouldn’ work. The result would be {index: [values]}.

>>> print(json.dumps(df[["x"]].values_to_dict(), indent=4))
{
    "0": [
        "A"
    ],
    "1": [
        "A"
    ],
    "2": [
        "B"
    ],
    "3": [
        "B"
    ],
    "4": [
        "B"
    ]
}

Unpack one element value list.

>>> print(json.dumps(df.values_to_dict(to_list=False), indent=4))
{
    "A": {
        "a": "1",
        "b": "2"
    },
    "B": {
        "c": "3",
        "d": [
            "3",
            "4"
        ]
    }
}