I;m trying to convert a python dictionary to contents of csv file where all keys will be column names and values of dict will be rows, using from_dict() throws a value error.
dict = { 'Name' : 'ABC', 'Number': 123}
df = pd.DataFrame.from_dict(dict)
print(df)
df.to_csv(r'C:\Users\Desktop\data.csv')
Error:
Traceback (most recent call last):
File "C:/Users/PycharmProjects/PythonTest/Test3.py", line 73, in <module>
df = pd.DataFrame.from_dict(epprom_data)
File "C:\Users\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\frame.py", line 1373, in from_dict
return cls(data, index=index, columns=columns, dtype=dtype)
File "C:\Users\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\frame.py", line 529, in __init__
mgr = init_dict(data, index, columns, dtype=dtype)
File "C:\Users\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\internals\construction.py", line 287, in init_dict
return arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
File "C:\Users\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\internals\construction.py", line 80, in arrays_to_mgr
index = extract_index(arrays)
File "C:\Users\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\internals\construction.py", line 391, in extract_index
raise ValueError("If using all scalar values, you must pass an index")
ValueError: If using all scalar values, you must pass an index
Desired output: Excel file with below contents:
Name Number
1 ABC 123