-2

I have a dataframe

`data = np.array([['val1', 'val2'], ['val3', 'val4']]) dataFrame = pd.DataFrame(data, columns = ['key1', 'key2'])

I need to conver it into json.

By default the outout looks like:

{"key1":{"0":"val1","1":"val3"},"key2":{"0":"val2","1":"val4"}} ` I need it in the following layout.

{'key1':['val1','val2'],'key2':['val3','val4']}

I have looked up the option on pandas and none of them give me the desired output.

yelo95
  • 1

1 Answers1

0

You might want to use to_dict and json.dumps:

import json

out = json.dumps(dataFrame.to_dict(orient='list'))

Output: '{"key1": ["val1", "val3"], "key2": ["val2", "val4"]}'

mozway
  • 194,879
  • 13
  • 39
  • 75