oTree Forum >

Read CSV file contains multiple variables for multiple participants in multiple rounds

#1 by Bochen

Hi all,

In my experiment, 8 participants have 2 variables that need to be different among participants and 40 rounds in a treatment, but the data structure should be the same across treatments, which means participants who have the same ID among different treatments have the same value sequence of the variables. 

So in total, 640 values should be pre-generated and stored in a CSV file. Do you know in which form data should be written in the file(for example, the names from columns 1-4 are ID, round, variable1, variable2?)? And how should this file be imported into oTree and got by each player in each round?

I have seen the demo of "treatments_from_spreadsheet" in "otree-snippets", it assigns values of variables to different participants, but for only one round. How to assign the values for multiple rounds? Thanks in advance!


#2 by BonnEconLab

I would save the variables for the different participants in separate CSV files. That is, one CSV file per participant. Hence, you would have 8 separate CSV files with 2 columns and 40 rows each.

In the ZIP archive attached to this post, you will find example code for 2 participants, that is, I include 2 CSV files, Data_for_player_1.csv and Data_for_player_2.csv. The extension to 8 participants is straightforward.

In the settings.py file, add


You can then use the following __init__.py (which uses pandas for importing the CSV files, so you may have to install pandas first, via pip3 install pandas):

from otree.api import *

import pandas

doc = """
Your app description

class C(BaseConstants):

    NAME_IN_URL = 'zzz_read_csv'
    NUM_ROUNDS = 40

def creating_session(subsession):

    if subsession.round_number == 1:
        for player in subsession.get_players():
            data_for_player = pandas.read_csv("./zzz_read_csv/Data_for_player_" + str(player.id_in_subsession) + ".csv")
            player.participant.var1_from_csv = data_for_player["var1"].tolist()
            player.participant.var2_from_csv = data_for_player["var2"].tolist()
            # One might want to add a check here that the lists are not shorter than C.NUM_ROUNDS!

class Subsession(BaseSubsession):


class Group(BaseGroup):


class Player(BasePlayer):

    var1 = models.FloatField()
    var2 = models.FloatField()


class MyPage(Page):

    def vars_for_template(player):
        player.var1 = player.participant.var1_from_csv[player.round_number - 1]
        player.var2 = player.participant.var2_from_csv[player.round_number - 1]

page_sequence = [

#3 by Bochen (edited )

Thanks for your answer! It works well and is really helpful :D

#4 by Bochen


I have a further question. 

Based on the experiment mentioned above, if 8 participants are divided into groups of 2 in the latter 20 rounds, and the 2 variables should be the same for the participants in the same group (in this case I define the variables under class Group), how to let values imported from CSV file got by each group in each round? Using the way similar to assigning list to player.participant seems not to work since group class does not have an attribute like this. And is the CSV file the same as Data_for_playerX or should it be a similar file Data_for_groupX?

Besides, I need to replicate the match structure of players in the experiment that has been conducted before. So actually participants who have the same ID among different treatments should have the same value sequence of the variables in individual rounds(1-20, which now has been done), in match rounds(round 21-40), as well as the same match results. How to realize all of these requirements? Thanks in advance!


Write a reply

Set forum username