Home > Software engineering >  If using all scalar values, you must pass a index? | pandas
If using all scalar values, you must pass a index? | pandas

Time:08-12

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!

CodePudding user response:

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']})

CodePudding user response:

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)
  • Related