2

I have a pandas dataframe with columns col1, col2 and col3 and respective values. I would need to transform column names and values into a JSON string.

For instance, if the dataset is

data= pd.DataFrame({'col1': ['bravo', 'charlie','price'], 'col2': [1, 2, 3],'col3':['alpha','beta','gamma']})

I need to obtain an output like this

newdata= pd.DataFrame({'index': [0,1,2], 'payload': ['{"col1":"bravo", "col2":"1", "col3":"alpha"}', '{"col1":"charlie", "col2":"2", "col3":"beta"}', '{"col1":"price", "col2":"3", "col3":"gamma"}']})

I didn't find any function or iterative tool to perform this.

Thank you in advance!

5 Answers 5

2

You can use:

df = data.agg(lambda s: dict(zip(s.index, s)), axis=1).rename('payload').to_frame()

Result:

# print(df)

                                          payload
0   {'col1': 'bravo', 'col2': 1, 'col3': 'alpha'}
1  {'col1': 'charlie', 'col2': 2, 'col3': 'beta'}
2   {'col1': 'price', 'col2': 3, 'col3': 'gamma'}
Sign up to request clarification or add additional context in comments.

Comments

2

Here you go:

import pandas as pd

data= pd.DataFrame({'col1': ['bravo', 'charlie','price'], 'col2': [1, 2, 3],'col3':['alpha','beta','gamma']})
new_data = pd.DataFrame({
    'payload': data.to_dict(orient='records')
})
print(new_data)

## -- End pasted text --
                                          payload
0   {'col1': 'bravo', 'col2': 1, 'col3': 'alpha'}
1  {'col1': 'charlie', 'col2': 2, 'col3': 'beta'}
2   {'col1': 'price', 'col2': 3, 'col3': 'gamma'}

Comments

1

If my understanding is correct, you want the index and the data records as a dict.

So:

dict(index=list(data.index), payload=data.to_dict(orient='records'))

For your example data:

>>> import pprint
>>> pprint.pprint(dict(index=list(data.index), payload=data.to_dict(orient='records')))
{'index': [0, 1, 2],
 'payload': [{'col1': 'bravo', 'col2': 1, 'col3': 'alpha'},
             {'col1': 'charlie', 'col2': 2, 'col3': 'beta'},
             {'col1': 'price', 'col2': 3, 'col3': 'gamma'}]}

Comments

1

This is one approach using .to_dict('index').

Ex:

import pandas as pd

data= pd.DataFrame({'col1': ['bravo', 'charlie','price'], 'col2': [1, 2, 3],'col3':['alpha','beta','gamma']})
newdata = data.to_dict('index')
print({'index': list(newdata.keys()), 'payload': list(newdata.values())})
#OR -->newdata= pd.DataFrame({'index': list(newdata.keys()), 'payload': list(newdata.values())})

Output:

{'index': [0, 1, 2],
 'payload': [{'col1': 'bravo', 'col2': 1, 'col3': 'alpha'},
             {'col1': 'charlie', 'col2': 2, 'col3': 'beta'},
             {'col1': 'price', 'col2': 3, 'col3': 'gamma'}]}

Comments

0

Use to_dict: newdata = data.T.to_dict()

>>> print(newdata.values())
[
  {'col2': 1, 'col3': 'alpha', 'col1': 'bravo'},
  {'col2': 2, 'col3': 'beta', 'col1': 'charlie'},
  {'col2': 3, 'col3': 'gamma', 'col1': 'price'}
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.