Missing headers in forms



Hi, I am trying out the form component from the builder, but I keep receiving errors saying I don’t have headers. I tried the solution posted from this topic, but it didn’t seem to work.

Attached files were screenshots of my error and the excel file.

I appreciate any helpful tips and insights.

Thank you very much.

Please could you upload your form csv file?

Hi @wakecarter
The excel file is attached below.
form_new.xlsx (14.4 KB)

Please note that the form component worked well when built with the form component by itself (see
form_test.psyexp (9.5 KB)), but when I inserted a code to display the form in two pages (see
panas_test.psyexp (24.9 KB)), PsychoPy will give me the error of missing headers.

The form component expects to receive the name of a csv file.

questions = pd.read_excel('form_new.xlsx')
questions.columns = questions.columns.str.strip()
all_questions = questions['itemText'].tolist()

random.shuffle(all_questions)

firstPANAS = all_questions[:8]
secondPANAS = all_questions[8:]

You seem to be giving it a list of 8 values for itemText

If you want to randomly shuffle your questions into two lists, I would recommend saving them to two correctly formatted csv files and then reloading them in the form component.

Thank you for your reply.
I wanted the form to be displayed in two pages, with 8 randomly displayed items from itemText on the first page, and the remaining 8 will be displayed on the second page. I have thought of creating two excel files, but in that case, I think I can’t randomly display the first 8 out of 16 on the first page with the remaining 8 displayed randomly on the next.

I also tried to change my form file into .csv with the

pd.read_csv(‘form_new.csv’)

command, but that also still gave me the missing headers error.

I meant write two csv files dynamically during the experiment, rather than preselecting them. You are already using pandas to read the questions, so it shouldn’t be too much extra to also use it to write new csv files.

Thank you for your suggestion, and sorry for the misunderstanding.
I tried to generate the csv files during the experiment, but it still gives me the error of missing headers.

new code:

import random
import openpyxl as op
import pandas as pd

questions = pd.read_csv('form_new.csv')
questions.columns = questions.columns.str.strip()
all_questions = questions['itemText'].tolist()

random.shuffle(all_questions)

firstPANAS = all_questions[:8]
secondPANAS = all_questions[8:]

firstPANAS_fl = pd.DataFrame({'itemText': firstPANAS})
firstPANAS_fl.to_csv('firstPANAS.csv', index=False)

secondPANAS_fl = pd.DataFrame({'itemText': secondPANAS})
secondPANAS_fl.to_csv('secondPANAS.csv', index=False)

form_new.csv (272.9 KB)
panas_test.psyexp (25.5 KB)

Please could you upload examples of the two csv files that were created?

So I tried out some code and the issue seemed to be fixed; I could have the first page randomly displaying 8 out of 16 questions while the second page randomly displays the rest.

import random
import pandas as pd

questions = pd.read_csv('form_new.csv')
questions.columns = questions.columns.str.strip()
all_questions = questions['itemText'].tolist()

random.shuffle(all_questions)

firstPANAS = all_questions[:8]
secondPANAS = all_questions[8:]

def create_csv(questions, filename):
    data = {
        'itemText': questions,       
        'type': ['rating'] * len(questions),  
        'options': ['1,2,3,4,5,6'] * len(questions),  
    }
    df = pd.DataFrame(data)
    df.to_csv(filename, index=False)

create_csv(firstPANAS, 'firstPANAS.csv')
create_csv(secondPANAS, 'secondPANAS.csv')

However, when I tried to loop for 5 times, every time (aka every trial), I get the same set of questions on the first and second page respectively. Even though I tried to randomly looped, PsychoPy will give me the same trial every time. Any idea why this happened?

original form file:
form_new.csv (272.9 KB)
two generated files during the experiment:
firstPANAS.csv (318 Bytes)
secondPANAS.csv (306 Bytes)
experiment file:
PANAS.psyexp (30.7 KB)

Do you really want to present the same questions multiple times to the same participants?

I suspect that the PsychoPy form doesn’t expect to have different csv files when used more than once, so it will get stored in memory once it has first been loaded. If you really want to do this then I think you’ll need to use a separate pair of form components for each presentation of the PANAS questions.

I see. I do want to present the same 16 questions multiple times to the same participants. I just need them to be randomized at each trial and each page.
Please correct me if I am wrong, so for the nature of the form component, maybe it is better if I create a different set of routines representing one trial instead of looping.

Yes. Normally I advocate minimising the number of unique routines but in this case I think you will need to have multiple routines instead of looping.

Alternatively you could set up the questions differently. e.g using my Survey Engine online demo but that only gives one question per page and would need some modification.

1 Like