Beispiel #1
0
table1 = [['id', 'sex', 'age', 'height', 'weight'],
          [1, 'male', 16, 1.45, 62.0],
          [2, 'female', 19, 1.34, 55.4],
          [3, '-', 17, 1.78, 74.4],
          [4, 'male', 21, 1.33]]

from petl import recordmapmany, look    
look(table1)
def rowgenerator(rec):
    transmf = {'male': 'M', 'female': 'F'}
    yield [rec['id'], 'gender', transmf[rec['sex']] if rec['sex'] in transmf else rec['sex']]
    yield [rec['id'], 'age_months', rec['age'] * 12]
    yield [rec['id'], 'bmi', rec['weight'] / rec['height'] ** 2]

table2 = recordmapmany(table1, rowgenerator, fields=['subject_id', 'variable', 'value'])  
look(table2)


# setheader

table1 = [['foo', 'bar'],
          ['a', 1],
          ['b', 2]]

from petl import setheader, look
look(table1)
table2 = setheader(table1, ['foofoo', 'barbar'])
look(table2)

Beispiel #2
0
table1 = [['id', 'sex', 'age', 'height', 'weight'],
          [1, 'male', 16, 1.45, 62.0],
          [2, 'female', 19, 1.34, 55.4],
          [3, '-', 17, 1.78, 74.4],
          [4, 'male', 21, 1.33]]

from petl import recordmapmany, look    
look(table1)
def rowgenerator(rec):
    transmf = {'male': 'M', 'female': 'F'}
    yield [rec['id'], 'gender', transmf[rec['sex']] if rec['sex'] in transmf else rec['sex']]
    yield [rec['id'], 'age_months', rec['age'] * 12]
    yield [rec['id'], 'bmi', rec['weight'] / rec['height'] ** 2]

table2 = recordmapmany(table1, rowgenerator, fields=['subject_id', 'variable', 'value'])  
look(table2)


# setheader

table1 = [['foo', 'bar'],
          ['a', 1],
          ['b', 2]]

from petl import setheader, look
look(table1)
table2 = setheader(table1, ['foofoo', 'barbar'])
look(table2)