File: issue_256.py

package info (click to toggle)
python-petl 1.7.17-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,224 kB
  • sloc: python: 22,617; makefile: 109; xml: 9
file content (43 lines) | stat: -rw-r--r-- 862 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# -*- coding: utf-8 -*-
# <nbformat>3.0</nbformat>

# <markdowncell>

# Notes supporting [issue #256](https://github.com/alimanfoo/petl/issues/256).

# <codecell>

import petl.interactive as etl

# <codecell>

t1 = etl.wrap([['foo', 'bar'], [1, 'a'], [2, 'b']])
t1

# <codecell>

t2 = etl.wrap([['foo', 'bar'], [1, 'a'], [2, 'c']])
t2

# <codecell>

t3 = etl.merge(t1, t2, key='foo')
t3

# <markdowncell>

# The problem with the above is that you cannot tell from inspecting *t3* alone which conflicting value comes from which source.
# 
# A workaround as suggested by [@pawl](https://github.com/pawl) is to use the [*conflicts()*](http://petl.readthedocs.org/en/latest/#petl.conflicts) function, e.g.: 

# <codecell>

t4 = (etl
    .cat(
        t1.addfield('source', 1),
        t2.addfield('source', 2)
    )
    .conflicts(key='foo', exclude='source')
)
t4