I’m using Python 3.11 and Pandas v2.2.2 on Windows 10.
When I assign my data to a dataframe like this
'''datadict = a dict of all my data with keys.
options = a class holding different data to pass between functions.
options.headersxl = ['Hdr1', 'hdr2', 'hdr3', 'hdr4']
'''
import pandas
import xlsxwriter
procname = str(inspect.stack()[0][3]) + ":" # Returns function name.
fn = os.path.join(options.progdir, options.datafn )
tfn = fn + ".xlsx"
if os.path.exists(tfn):
fn + "_" + getdatetime()
fn + ".xlsx"
outdata = []
sheetname = 'Sheet1'
# Now append rest of data row-by-row.
for key, val in datadict.items():
# tlist = [key.split(',') for key in datadict] # Split one row
t = datadict[key]
tlist = t.split(DATASEP) # Convert one row to a list.
outdata.append(tlist) # Save one row
try:
df=pandas.DataFrame(outdata, columns=options.headersxl) # Make a dataframe from the list of lists.
except ValueError as e:
sys.exit(f"{procname} ERROR-dataframe1: Num headers does not match num columns.")
except Exception as e:
sys.exit(f"{procname} ERROR-dataframe2: {e}")
I get an error “ValueError: 15 columns passed, passed data had 16 columns”.
- How do I avoid this error? The data in every row may not have the same number of columns that the headers do.
Thank you.