« Restructurer un tableau de données dyadiques » : différence entre les versions

De EduTech Wiki
Aller à la navigation Aller à la recherche
Aucun résumé des modifications
Aucun résumé des modifications
Ligne 7 : Ligne 7 :
Individual structure :
Individual structure :


[[File:attachment:Attachment-dyadic_individual_structure.gif|frame|none|alt=|caption dyadic_individual_structure.gif]]
[[File:Attachment-dyadic_individual_structure.gif|Individual structure]]


Dyadic structure :
Dyadic structure :


[[File:attachment:dyadic_dyadic_structure.gif|frame|none|alt=|caption dyadic_dyadic_structure.gif]]
[[File:Attachment-dyadic_individual_structure.gif|Individual structure]]
[[Fichier:Dyadic structure.gif|Dyadic structure]]


= <b>Code</b> =
= <b>Code</b> =

Version du 16 novembre 2018 à 11:18

Restructure data table from individual to dyadic structure

This script requires python3+ and pandas library.

Install pandas with pip : python -m pip install –upgrade pandas in command prompt/terminal

Individual structure :

Individual structure

Dyadic structure :

Individual structure Dyadic structure

Code

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import pandas as pd

Specify folder path

os.chdir('D:\\Google Drive\\1-These\\13-EATMINT2\\Analyses\\Questionnaires')

Specify .csv file name

df1 = pd.read_csv('questionnaire_raw_data_individual_structure.csv',sep=';')

Delete columns without data structure change needed

data = df1.drop(['Dyade','Sujet','Sexe','Condition'], axis = 1)

Put columns names in a list

variables = data.columns

Create a new data frame

new_data = pd.DataFrame()

For each column, get even values and put them in p1 (resp. odd values in p2). Give the same index to both p1 and p2. Add both renamed columns in the data frame

for i in variables:
    p1 = data[i].iloc[::2]
    p1.index = range(1,len(p1)+1)
    
    p2 = data[i].iloc[1::2]
    p2.index = range(1,len(p2)+1)

    v = i
    v1 = i + '_p1'
    v2 = i + '_p2'
    
    new_data[v1] = p1
    new_data[v2] = p2

Export restructured data

new_data.to_csv('out.csv',sep=';')