Use Series.to_json and if necessary change key value add rename:
print (k.set_index('A').rename(columns={'B':'index1'}).to_json())
{"index1":{"1":"a","2":"b","3":"c","4":"d"}}
If need export to file:
k.set_index('A').rename(columns={'B':'index1'}).to_json('file.json')
Answer from jezrael on Stack OverflowUse Series.to_json and if necessary change key value add rename:
print (k.set_index('A').rename(columns={'B':'index1'}).to_json())
{"index1":{"1":"a","2":"b","3":"c","4":"d"}}
If need export to file:
k.set_index('A').rename(columns={'B':'index1'}).to_json('file.json')
Although what I am writing is not the answer to the question asked, still I am providing a solution to a small problem I was facing which I googled and reached here.
Problem: how to create a dictionary from a panda data frame with a column as the key and constant value (1 in my case) as the, you guessed it, value.
Solution:
f = pd.Series(data = [1]*df.shape[0],index=df['col_name'])
x = f.to_json(orient='columns')
Output:
{"one":1, "two":1, "three": 1}
Why would I do that? Because search in the dictionary is highly optimized (Yeah I can use set as well)
P.S. Novice in Python so please be gentle with me :).
python - Using column values as key in pandas json - Stack Overflow
python - convert pandas dataframe to json with columns as key - Stack Overflow
Convert Pandas DataFrame to JSON format - Stack Overflow
python - Transform pandas DataFrame to json and add new keys - Stack Overflow
In newer versions of pandas (0.20.0+, I believe), this can be done directly:
df.to_json('temp.json', orient='records', lines=True)
Direct compression is also possible:
df.to_json('temp.json.gz', orient='records', lines=True, compression='gzip')
The output that you get after DF.to_json is a string. So, you can simply slice it according to your requirement and remove the commas from it too.
out = df.to_json(orient='records')[1:-1].replace('},{', '} {')
To write the output to a text file, you could do:
with open('file_name.txt', 'w') as f:
f.write(out)
Pandas is equipped for this out of the box.
pandas.DataFrame.to_json
here is the example dataframe:
import json
df = pd.DataFrame(
[["a", "b"], ["c", "d"]],
index=["row 1", "row 2"],
columns=["col 1", "col 2"],
)
Here is the result using to_json():
result = df.to_json(orient="split")
parsed = json.loads(result)
json.dumps(parsed, indent=4)
{
"columns": [
"col 1",
"col 2"
],
"index": [
"row 1",
"row 2"
],
"data": [
[
"a",
"b"
],
[
"c",
"d"
]
]
}
here is the link: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_json.html
As per the function provided here @Parsa T. You can just change the column names and use the function to get the required result.
def set_for_keys(my_dict, key_arr, val):
"""
Set value at the path in my_dict defined by the string (or serializable object) array key_arr
"""
current = my_dict
for i in range(len(key_arr)):
key = key_arr[i]
if key not in current:
current[key] = val if i==len(key_arr)-1 else {}
else:
if type(current[key]) is not dict:
print("Given dictionary is not compatible with key structure requested")
raise ValueError("Dictionary key already occupied")
current = current[key]
return my_dict
def to_formatted_json(df, sep="."):
result = []
for _, row in df.iterrows():
parsed_row = {}
for idx, val in row.iteritems():
keys = idx.split(sep)
parsed_row = set_for_keys(parsed_row, keys, val)
result.append(parsed_row)
return result
df.columns = ['ID', 'PERSONAL.NAME', 'PERSONAL.LAST', 'GEO.ADDRESS', 'GEO.COUNTY']
#Where df was parsed from json-dict using json_normalize
print(to_formatted_json(df, sep="."))
OUTPUT:
[{'ID': '0',
'PERSONAL': {'NAME': 'jimmy', 'LAST': 'neutron'},
'GEO': {'ADDRESS': '101 ocean avenue', 'COUNTY': 'yellow card park'}},
{'ID': '1',
'PERSONAL': {'NAME': 'james', 'LAST': 'baxter'},
'GEO': {'ADDRESS': '202 bubble gum county', 'COUNTY': 'candy kingdom'}},
{'ID': '2',
'PERSONAL': {'NAME': 'joben', 'LAST': 'segel'},
'GEO': {'ADDRESS': '303 china town', 'COUNTY': 'universal studio'}}]