« 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 1 : Ligne 1 :
[[Fichier:indivdual_to_dyadic_structure.pdf|vignette]]
= 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 :
 
[[File:attachment:dyadic_individual_structure.gif|frame|none|alt=|caption dyadic_individual_structure.gif]]
 
Dyadic structure :
 
[[File:attachment:dyadic_dyadic_structure.gif|frame|none|alt=|caption dyadic_dyadic_structure.gif]]
 
= <b>Code</b> =
 
<source lang="python">#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import pandas as pd</source>
<b>Specify folder path</b>
 
<source lang="python">os.chdir('D:\\Google Drive\\1-These\\13-EATMINT2\\Analyses\\Questionnaires')</source>
<b>Specify .csv file name</b>
 
<source lang="python">df1 = pd.read_csv('questionnaire_raw_data_individual_structure.csv',sep=';')</source>
<b>Delete columns without data structure change needed</b>
 
<source lang="python">data = df1.drop(['Dyade','Sujet','Sexe','Condition'], axis = 1)</source>
<b>Put columns names in a list</b>
 
<source lang="python">variables = data.columns</source>
<b>Create a new data frame</b>
 
<source lang="python">new_data = pd.DataFrame()</source>
<b>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</b>
 
<source lang="python">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</source>
<b>Export restructured data</b>
 
<source lang="python">new_data.to_csv('out.csv',sep=';')</source>


[[category: Python]]
[[category: Python]]

Version du 16 novembre 2018 à 11:05

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 :

Fichier:Attachment:dyadic individual structure.gif
caption dyadic_individual_structure.gif

Dyadic structure :

Fichier:Attachment:dyadic dyadic structure.gif
caption dyadic_dyadic_structure.gif

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=';')