Carolina
Carolina

Reputation: 1

How to import experiment data to an Excel file?

I'm writing my first experiment on Python. I have created my experiment using Psychopy and everything is working but I'm having problems to import the data from my experiment in Python to an Excel file.

My Excel file is blank once I'm done running my experiment. I will share the lines of code in which I try to import the data to Excel.

Thank you for your help!

df = pandas.DataFrame(columns=['answer', 'correctness', 'RT']) 
df.loc[csvcounter] = [user_response[0][0], correctness, user_response[0][1]] #[0],[0] ... [0][1]
df = pandas.DataFrame(columns=['answer', 'correctness', 'RT']) 
df.to_csv(FILE_DIR + r"/Experiment.csv")

Upvotes: 0

Views: 62

Answers (1)

Jurgy
Jurgy

Reputation: 2360

Your third line creates a new empty DataFrame and overwrites the variable with the filled DataFrame. Next, you write the empty DataFrame to a csv file.


Answer to question in the comments:
Depends how your experiment's data is structured. But here's an example for numpy.ndarray: Assume we got data with 1) the participants number, 2) their gender and 3) their age:

import numpy as np
import pandas as pd

data = np.array([[1,2,3,4], ['M', 'F', 'F', 'M'], [18, 21, 35, 27]]).T

so data now looks like this:

array([['1', 'M', '18'],
       ['2', 'F', '21'],
       ['3', 'F', '35'],
       ['4', 'M', '27']], ='<U11')

now we create an empty DataFrame similar to what you did:

df = pd.DataFrame({'number':[], 'gender':[], 'age':[]})

and write the ndarray to it:

df[['number', 'gender', 'age']] = data

And if you write this DataFrame to a file it should contain the 4 people:

df.to_csv(FILE_DIR + r"/Experiment.csv")

Upvotes: 1

Related Questions