« 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
(Page blanchie)
Balise : Blanchiment
Ligne 1 : Ligne 1 :


Ce script python est destiné à convertir un fichier de données dyadiques d'une structure individuelle à une structure dyadique. Il nécessite python3+ et la librairie pandas.
Pour installer pandas : python -m pip install --upgrade pandas dans le terminal.
Structure individuelle :
![dyadic_individual_structure.gif](attachment:dyadic_individual_structure.gif)
Structure dyadique :
![dyadic_dyadic_structure.gif](attachment:dyadic_dyadic_structure.gif)
# <b>Code</b>
```python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import pandas as pd
```
<b>Indiquer le chemin du dossier contenant le fichier .csv à la place de path (entre guillemets)</b>
```python
os.chdir('D:\\Google Drive\\1-These\\13-EATMINT2\\Analyses\\Questionnaires')
```
<b>Indiquer le nom du fichier .csv à la place de file (entre guillemets)</b>
```python
df1 = pd.read_csv('questionnaire_raw_data_individual_structure.csv',sep=';')
```
Voir les premières lignes de votre fichier :
```python
df1.head()
```
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }
    .dataframe tbody tr th {
        vertical-align: top;
    }
    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Dyade</th>
      <th>Sujet</th>
      <th>Sexe</th>
      <th>Condition</th>
      <th>Score Raven</th>
      <th>Activation_soi</th>
      <th>Anxiété_soi</th>
      <th>Colère_soi</th>
      <th>Contrôle_soi</th>
      <th>Déception_soi</th>
      <th>...</th>
      <th>Mon partenaire mettait nos différences en évidence</th>
      <th>Mon partenaire n'était pas réceptif à ce que je lui disais</th>
      <th>Mon partenaire n'était pas très sûr de lui</th>
      <th>Mon partenaire ne se fiait pas à ce que je lui proposais</th>
      <th>Mon partenaire ne semblait pas enthousiasmé par nos échanges</th>
      <th>Mon partenaire se comportait comme s'il était plus fort que moi</th>
      <th>Mon partenaire se montrait aimable envers moi</th>
      <th>Mon partenaire se montrait autant intéressé par la construction d'une bonne entente entre nous que par la résolution de la tâche</th>
      <th>Mon partenaire se montrait tendu et\ou agacé</th>
      <th>Mon partenaire voulait rester coller à l'objectif principal de la tâche</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1</td>
      <td>1</td>
      <td>H</td>
      <td>CBVB</td>
      <td>20.0</td>
      <td>4</td>
      <td>1.0</td>
      <td>3</td>
      <td>2.0</td>
      <td>6.0</td>
      <td>...</td>
      <td>1.0</td>
      <td>2.0</td>
      <td>5.0</td>
      <td>3</td>
      <td>1</td>
      <td>1.0</td>
      <td>7</td>
      <td>6</td>
      <td>1</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>1</th>
      <td>1</td>
      <td>2</td>
      <td>H</td>
      <td>CBVB</td>
      <td>22.0</td>
      <td>4</td>
      <td>1.0</td>
      <td>1</td>
      <td>2.0</td>
      <td>5.0</td>
      <td>...</td>
      <td>1.0</td>
      <td>1.0</td>
      <td>2.0</td>
      <td>1</td>
      <td>1</td>
      <td>1.0</td>
      <td>7</td>
      <td>7</td>
      <td>1</td>
      <td>6.0</td>
    </tr>
    <tr>
      <th>2</th>
      <td>2</td>
      <td>1</td>
      <td>H</td>
      <td>CBVH</td>
      <td>19.0</td>
      <td>5</td>
      <td>2.0</td>
      <td>3</td>
      <td>4.0</td>
      <td>2.0</td>
      <td>...</td>
      <td>2.0</td>
      <td>1.0</td>
      <td>2.0</td>
      <td>1</td>
      <td>1</td>
      <td>1.0</td>
      <td>7</td>
      <td>7</td>
      <td>4</td>
      <td>7.0</td>
    </tr>
    <tr>
      <th>3</th>
      <td>2</td>
      <td>2</td>
      <td>H</td>
      <td>CBVH</td>
      <td>17.0</td>
      <td>4</td>
      <td>3.0</td>
      <td>1</td>
      <td>5.0</td>
      <td>1.0</td>
      <td>...</td>
      <td>1.0</td>
      <td>2.0</td>
      <td>2.0</td>
      <td>3</td>
      <td>1</td>
      <td>5.0</td>
      <td>6</td>
      <td>7</td>
      <td>2</td>
      <td>4.0</td>
    </tr>
    <tr>
      <th>4</th>
      <td>3</td>
      <td>1</td>
      <td>H</td>
      <td>CHVB</td>
      <td>23.0</td>
      <td>4</td>
      <td>6.0</td>
      <td>2</td>
      <td>3.0</td>
      <td>6.0</td>
      <td>...</td>
      <td>5.0</td>
      <td>1.0</td>
      <td>4.0</td>
      <td>1</td>
      <td>1</td>
      <td>1.0</td>
      <td>6</td>
      <td>7</td>
      <td>4</td>
      <td>5.0</td>
    </tr>
  </tbody>
</table>
<p>5 rows × 125 columns</p>
</div>
<b>Supprimer les colonnes pour lesquelles le changement de structure ne s'applique pas</b>
```python
data = df1.drop(['Dyade','Sujet','Sexe','Condition'], axis = 1)
data.head()
```
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }
    .dataframe tbody tr th {
        vertical-align: top;
    }
    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Score Raven</th>
      <th>Activation_soi</th>
      <th>Anxiété_soi</th>
      <th>Colère_soi</th>
      <th>Contrôle_soi</th>
      <th>Déception_soi</th>
      <th>Désespoir_soi</th>
      <th>Ennui_soi</th>
      <th>Espoir_soi</th>
      <th>Fierté_soi</th>
      <th>...</th>
      <th>Mon partenaire mettait nos différences en évidence</th>
      <th>Mon partenaire n'était pas réceptif à ce que je lui disais</th>
      <th>Mon partenaire n'était pas très sûr de lui</th>
      <th>Mon partenaire ne se fiait pas à ce que je lui proposais</th>
      <th>Mon partenaire ne semblait pas enthousiasmé par nos échanges</th>
      <th>Mon partenaire se comportait comme s'il était plus fort que moi</th>
      <th>Mon partenaire se montrait aimable envers moi</th>
      <th>Mon partenaire se montrait autant intéressé par la construction d'une bonne entente entre nous que par la résolution de la tâche</th>
      <th>Mon partenaire se montrait tendu et\ou agacé</th>
      <th>Mon partenaire voulait rester coller à l'objectif principal de la tâche</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>20.0</td>
      <td>4</td>
      <td>1.0</td>
      <td>3</td>
      <td>2.0</td>
      <td>6.0</td>
      <td>2.0</td>
      <td>5</td>
      <td>NaN</td>
      <td>3</td>
      <td>...</td>
      <td>1.0</td>
      <td>2.0</td>
      <td>5.0</td>
      <td>3</td>
      <td>1</td>
      <td>1.0</td>
      <td>7</td>
      <td>6</td>
      <td>1</td>
      <td>3.0</td>
    </tr>
    <tr>
      <th>1</th>
      <td>22.0</td>
      <td>4</td>
      <td>1.0</td>
      <td>1</td>
      <td>2.0</td>
      <td>5.0</td>
      <td>2.0</td>
      <td>1</td>
      <td>5.0</td>
      <td>2</td>
      <td>...</td>
      <td>1.0</td>
      <td>1.0</td>
      <td>2.0</td>
      <td>1</td>
      <td>1</td>
      <td>1.0</td>
      <td>7</td>
      <td>7</td>
      <td>1</td>
      <td>6.0</td>
    </tr>
    <tr>
      <th>2</th>
      <td>19.0</td>
      <td>5</td>
      <td>2.0</td>
      <td>3</td>
      <td>4.0</td>
      <td>2.0</td>
      <td>1.0</td>
      <td>1</td>
      <td>3.0</td>
      <td>5</td>
      <td>...</td>
      <td>2.0</td>
      <td>1.0</td>
      <td>2.0</td>
      <td>1</td>
      <td>1</td>
      <td>1.0</td>
      <td>7</td>
      <td>7</td>
      <td>4</td>
      <td>7.0</td>
    </tr>
    <tr>
      <th>3</th>
      <td>17.0</td>
      <td>4</td>
      <td>3.0</td>
      <td>1</td>
      <td>5.0</td>
      <td>1.0</td>
      <td>2.0</td>
      <td>1</td>
      <td>7.0</td>
      <td>5</td>
      <td>...</td>
      <td>1.0</td>
      <td>2.0</td>
      <td>2.0</td>
      <td>3</td>
      <td>1</td>
      <td>5.0</td>
      <td>6</td>
      <td>7</td>
      <td>2</td>
      <td>4.0</td>
    </tr>
    <tr>
      <th>4</th>
      <td>23.0</td>
      <td>4</td>
      <td>6.0</td>
      <td>2</td>
      <td>3.0</td>
      <td>6.0</td>
      <td>2.0</td>
      <td>1</td>
      <td>2.0</td>
      <td>5</td>
      <td>...</td>
      <td>5.0</td>
      <td>1.0</td>
      <td>4.0</td>
      <td>1</td>
      <td>1</td>
      <td>1.0</td>
      <td>6</td>
      <td>7</td>
      <td>4</td>
      <td>5.0</td>
    </tr>
  </tbody>
</table>
<p>5 rows × 121 columns</p>
</div>
<b>Mettre les noms des colonnes dans une variable</b>
```python
variables = data.columns
```
<b>Créer un nouveau tableau de données</b>
```python
new_data = pd.DataFrame()
```
<b>Pour chaque colonne, mettre dans p1 les valeurs paires et dans p2 les valeurs p2.
Donner un index identique pour p1 et p2.
Ajouter les deux colonnes à la suite dans le tableau de données</b>
```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
```
```python
new_data.head()
```
<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }
    .dataframe tbody tr th {
        vertical-align: top;
    }
    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>Score Raven_p1</th>
      <th>Score Raven_p2</th>
      <th>Activation_soi_p1</th>
      <th>Activation_soi_p2</th>
      <th>Anxiété_soi_p1</th>
      <th>Anxiété_soi_p2</th>
      <th>Colère_soi_p1</th>
      <th>Colère_soi_p2</th>
      <th>Contrôle_soi_p1</th>
      <th>Contrôle_soi_p2</th>
      <th>...</th>
      <th>Mon partenaire se comportait comme s'il était plus fort que moi_p1</th>
      <th>Mon partenaire se comportait comme s'il était plus fort que moi_p2</th>
      <th>Mon partenaire se montrait aimable envers moi_p1</th>
      <th>Mon partenaire se montrait aimable envers moi_p2</th>
      <th>Mon partenaire se montrait autant intéressé par la construction d'une bonne entente entre nous que par la résolution de la tâche_p1</th>
      <th>Mon partenaire se montrait autant intéressé par la construction d'une bonne entente entre nous que par la résolution de la tâche_p2</th>
      <th>Mon partenaire se montrait tendu et\ou agacé_p1</th>
      <th>Mon partenaire se montrait tendu et\ou agacé_p2</th>
      <th>Mon partenaire voulait rester coller à l'objectif principal de la tâche_p1</th>
      <th>Mon partenaire voulait rester coller à l'objectif principal de la tâche_p2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>1</th>
      <td>20.0</td>
      <td>22.0</td>
      <td>4</td>
      <td>4</td>
      <td>1.0</td>
      <td>1.0</td>
      <td>3</td>
      <td>1</td>
      <td>2.0</td>
      <td>2.0</td>
      <td>...</td>
      <td>1.0</td>
      <td>1.0</td>
      <td>7</td>
      <td>7</td>
      <td>6</td>
      <td>7</td>
      <td>1</td>
      <td>1</td>
      <td>3.0</td>
      <td>6.0</td>
    </tr>
    <tr>
      <th>2</th>
      <td>19.0</td>
      <td>17.0</td>
      <td>5</td>
      <td>4</td>
      <td>2.0</td>
      <td>3.0</td>
      <td>3</td>
      <td>1</td>
      <td>4.0</td>
      <td>5.0</td>
      <td>...</td>
      <td>1.0</td>
      <td>5.0</td>
      <td>7</td>
      <td>6</td>
      <td>7</td>
      <td>7</td>
      <td>4</td>
      <td>2</td>
      <td>7.0</td>
      <td>4.0</td>
    </tr>
    <tr>
      <th>3</th>
      <td>23.0</td>
      <td>21.0</td>
      <td>4</td>
      <td>4</td>
      <td>6.0</td>
      <td>4.0</td>
      <td>2</td>
      <td>5</td>
      <td>3.0</td>
      <td>3.0</td>
      <td>...</td>
      <td>1.0</td>
      <td>1.0</td>
      <td>6</td>
      <td>7</td>
      <td>7</td>
      <td>7</td>
      <td>4</td>
      <td>1</td>
      <td>5.0</td>
      <td>7.0</td>
    </tr>
    <tr>
      <th>4</th>
      <td>21.0</td>
      <td>21.0</td>
      <td>4</td>
      <td>4</td>
      <td>3.0</td>
      <td>3.0</td>
      <td>2</td>
      <td>3</td>
      <td>5.0</td>
      <td>NaN</td>
      <td>...</td>
      <td>5.0</td>
      <td>1.0</td>
      <td>7</td>
      <td>6</td>
      <td>6</td>
      <td>7</td>
      <td>1</td>
      <td>2</td>
      <td>6.0</td>
      <td>6.0</td>
    </tr>
    <tr>
      <th>5</th>
      <td>23.0</td>
      <td>22.0</td>
      <td>4</td>
      <td>4</td>
      <td>1.0</td>
      <td>2.0</td>
      <td>1</td>
      <td>1</td>
      <td>4.0</td>
      <td>2.0</td>
      <td>...</td>
      <td>4.0</td>
      <td>2.0</td>
      <td>7</td>
      <td>6</td>
      <td>7</td>
      <td>7</td>
      <td>1</td>
      <td>2</td>
      <td>5.0</td>
      <td>6.0</td>
    </tr>
  </tbody>
</table>
<p>5 rows × 242 columns</p>
</div>
<b>Exporter le fichier .csv restructuré</b>
```python
new_data.to_csv('out.csv',sep=';')
```

Version du 15 novembre 2018 à 17:24