0

when i try this code:

def exportData(name_ofData, description_ofData):#save data and write to file function
    pd.DataFrame({'Date':dt.date.today(), 'Name': name_ofData, 'Description':description_ofData, 'Category':'1D'})

    raw_data.to_csv(r'C:\Users\peter\Documents\coding\python\projects\In progress\Notesreview\StudyData.csv', mode='a', index=False, header=False)

this error occurs:

Traceback (most recent call last):
  File "c:\Users\peter\Documents\coding\python\projects\In progress\Notesreview\notesreview.py", line 23, in <module>
    exportData('1', 2)
  File "c:\Users\peter\Documents\coding\python\projects\In progress\Notesreview\notesreview.py", line 8, in exportData
    pd.DataFrame({'Date':dt.date.today(), 'Name': name_ofData, 'Description':description_ofData, 'Category':'1D'})
  File "C:\Users\peter\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\frame.py", line 636, in __init__
    mgr = dict_to_mgr(data, index, columns, dtype=dtype, copy=copy, typ=manager)
  File "C:\Users\peter\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\internals\construction.py", line 502, in dict_to_mgr
    return arrays_to_mgr(arrays, columns, index, dtype=dtype, typ=typ, consolidate=copy)
  File "C:\Users\peter\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\internals\construction.py", line 120, in arrays_to_mgr
    index = _extract_index(arrays)
  File "C:\Users\peter\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\internals\construction.py", line 664, 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

why does this happen? all im trying to do is append a new row to a existing csv. Any fixes greatly appreciated!

  • Then why on earth would you involve pandas in this? Just open the file and do `print`. You don't need the enormous overhead that pandas brings in. – Tim Roberts Aug 11 '22 at 21:49
  • Does this answer your question? [Constructing pandas DataFrame from values in variables gives "ValueError: If using all scalar values, you must pass an index"](https://stackoverflow.com/questions/17839973/constructing-pandas-dataframe-from-values-in-variables-gives-valueerror-if-usi) – GodWin1100 Aug 11 '22 at 21:52
  • @TimRoberts i need to use pandas because later on i need to seperate the data again –  Aug 11 '22 at 21:55

3 Answers3

1

All your column values are scalars i.e like this :

df = pd.DataFrame({"A":1,"B":2})

Would throw

ValueError: If using all scalar values, you must pass an index

Whereas this would work

df = pd.DataFrame({"A":[1],"B":[2]})
print(df)

   A  B
0  1  2

In your case, these are all scalars (like first case)

{'Date':dt.date.today(), 'Name': name_ofData, 'Description':description_ofData, 'Category':'1D'}

Instead, do

pd.DataFrame({'Date':[dt.date.today()], 'Name': [name_ofData], 'Description':[description_ofData], 'Category':['1D']})
Sruthi
  • 2,908
  • 1
  • 11
  • 25
0

You definitely do not need pandas for this. If there are quotes or newlines, then you might need to do some quoting.

def exportData(name_ofData, description_ofData):#save data and write to file function
    with open(r'C:\Users\peter\Documents\coding\python\projects\In progress\Notesreview\StudyData.csv', 'a') as fd:
        print(f"{dt.date.today()},{name_ofData},{description_ofData},1D", file=fd)
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
0

Probably, the easiest way for your case would be creating an empty DataFrame and then appending your dictionary -- you must pass ignore_index as True for the dictionary to get appended to the dataframe:

df = pd.DataFrame()
df = df.append(your_python_dict, ignore_index=True)
jvel07
  • 1,239
  • 11
  • 15