cleverdict icon indicating copy to clipboard operation
cleverdict copied to clipboard

Add feature: import from/export to Pandas dataframe

Open PFython opened this issue 3 years ago • 3 comments

PFython avatar Feb 02 '22 14:02 PFython

This can be achieved using built-in Pandas functions.

To convert DataFrame to CleverDict

>>> df.to_dict(orient='index', into=CleverDict())

To convert CleverDict to DataFrame

>>> pd.DataFrame.from_dict(cleverdict_data, orient='index')

Hence this functionality will not add a lot of value to this library. If you had something else in mind, please add a description with an example of expected input/output.

gouravkr avatar Feb 05 '22 14:02 gouravkr

@gouravkr Many thanks for being so helpful and specific - much appreciated! Since it's an easy one-liner (each way) I think it'll be helpful to just add these examples to the README. I'm happy to do this or if you'd like to be credited as a contributor, feel free to fork and submit a pull request adding to README. All the best, Pete.

PFython avatar Feb 06 '22 05:02 PFython

While it would be a good idea to add these examples to the readme, I think there's some scope to implement better support for it. For instance, consider the following table:

name price
ice cream $2.50
pastry $3.50

Converting this to CleverDict using >>> c_dict = df.to_dict(orient='index', into=CleverDict()) would not allow me to refer to a column using the header. So I would not be able to use c_dict._0.name

Adding support for this would be a good idea.

gouravkr avatar Feb 09 '22 08:02 gouravkr