Is it possible to write json to file without index values and nesting using any Python library function e.g. pandas.DataFrame.to_json? My dataframe is only ever going to have one row, and the system that parses it cannot handle index values and requires the structure to be in this specific way.
What I want to achieve:
{"A":"D","B":"F","C":"G"}
Code example:
import pandas as pd
data = [["D", "F", "G"]]
df = pd.DataFrame(data, columns = ["A", "B", "C"])
result = df.to_json(orient="columns")
with open("test.txt", "w") as f:
f.write(result)
Tried so far:
df.to_json(orient="columns")
# {"A":{"0":"D"},"B":{"0":"F"},"C":{"0":"G"}}
df.to_json(orient="records")
# [{"A":"D","B":"F","C":"G"}]
df.to_json(orient="split")
# {"columns":["A","B","C"],"index":[0],"data":[["D","F","G"]]}
While what I want looks suspiciously like Python dictionary, I still want to know how to write to the file, and writing a dictionary {"A":"D","B":"F","C":"G"} to file throws
TypeError: write() argument must be str, not dict
str()on it, or use.to_json()instead of.to_dict()