Metadata object - how to convert to dictionary or Pandas DataFrame? #228
Unanswered
andrew-d-moses
asked this question in
Q&A
Replies: 3 comments
-
Morning, This is a function that I wrote to convert the metadata to json
|
Beta Was this translation helpful? Give feedback.
0 replies
-
metadata is an ordinary python object, therefore you can easily transform it to a dict using the vars function:
and with that you can convert it to json:
|
Beta Was this translation helpful? Give feedback.
0 replies
-
Fantastic, thank you for the prompt reply! This is very helpful!
Andy Moses
…On Thu, May 11, 2023, 3:47 AM James Anthony ***@***.***> wrote:
Morning,
If you haven't already read the docs, this is where I would start.
https://ofajardo.github.io/pyreadstat_documentation/_build/html/index.html
This is a function that I wrote to convert the metadata to json
def metadata_to_json(metadata):
"""
Function to turn metadata into json object.
:param: metadata dataframe to redeuce memory consumption
:return: json
"""
metadata_json = {
"column_names": metadata.column_names,
"column_labels": metadata.column_labels,
"column_names_to_labels": metadata.column_names_to_labels,
"number_columns": metadata.number_columns,
"variable_value_labels": metadata.variable_value_labels,
"value_labels": metadata.value_labels,
"variable_to_label": metadata.variable_to_label,
"original_variable_types": metadata.original_variable_types,
"readstat_variable_types": metadata.readstat_variable_types,
"variable_alignment": metadata.variable_alignment,
"variable_storage_width": metadata.variable_storage_width,
"variable_display_width": metadata.variable_display_width,
"variable_measure": metadata.variable_measure
}
return json.dumps(metadata_json)
—
Reply to this email directly, view it on GitHub
<#228 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/APE2KVTPM2GNTQRH4TEYRE3XFSRQZANCNFSM6AAAAAAX5KDFCI>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi all,
I'm having a difficult time wrapping my head around exactly what the metadata object is, and I'd love to be able to convert it to a more familiar format, such as a dictionary or a Pandas DataFrame. Additionally, I'm passing this to the next Django view via a session variable, so it should be serialized to some degree and converted into a format which can be passed. Any ideas? I'm not very familiar with JSON or BytesIO, for example, but will absolutely continue researching it if it's an option and somebody can get me started in the right direction. Below is a brief example of what I'd like to do:
Beta Was this translation helpful? Give feedback.
All reactions