Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13951

Pandas Dataframe to jsonlines grouping by columns

$
0
0
data = {'my_index': [1, 2],'start': ['2023-12-28 00:00:00', '2023-12-29 00:00:00'],'target': [['value1', 'value2'], ['value3']],'dynamic_feat': [[['feat1', 'feat2'], ['feat3']], [['feat4']]]}df = pd.DataFrame(data)pd.DataFrame.from_dict(data)

expected jsonlines format:

{"my_index": 1, "features": {"start": "2023-12-28 00:00:00", "target": [], "dynamic_feat": [[]]                }}...

I have a data that requires 1 column to be independent key while the rest of the columns to be grouped as "features". What is the best way to achieve this?

The data is huge so I can't do simple iteration to achieve this. Initially I used to_json to create the portion of features, but now I need to match it back to my_index. Not sure if there is any sleek way to do this!


Viewing all articles
Browse latest Browse all 13951

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>