Home>

I'm trying to find something in common between two words in Excel.

Word group 1 is a, b, c, d, e, f, g
Word group 2 is a, c, d, g
If so
a → o
b → x
c → o
d → o
e → x
f → x
g → o
I am trying to display it like this.

Error message

Word group 1 is d1, word group 2 is d2 (each is a Series),

lis = []
for i in range (d2.shape [0]):
lis.append (d2.iloc [i])
for j in range (d1.shape [0]):
if d1.iloc [j] in lis [-1]:
print (d1.iloc [j] +'\ t'+'o')
else:
print (d1.iloc [j] +'\ t'+'x')
And run
a → o
b → x
c → x
d → x
e → x
f → x
g → x
a → x
b → x
c → o
d → x
e → x
f → x
g → x
...
And a to g were repeated four times, and the result was not what I wanted.
If i know any good way, I would like you to tell me.

Applicable source code

lis = []
for i in range (df2.shape [0]):
lis.append (df2.iloc [i])
for j in range (df1.shape [0]):
if df1.iloc [j] in lis [-1]:
print (df1.iloc [j] +'\ t'+'o')
else:
print (df1.iloc [j] +'\ t'+'x')

Tried

I created two sets with lis [-1] and tried to solve them using sets. However, when set (lis [-1]) was used, the word was broken into one set of characters, Didn't work ...

Supplemental information (FW/tool version etc.)

Jupyter imports pandas. python3 series.

  • Answer # 1

    import pandas as pd
    d1 = pd.Series (["a", "b", "c", "d", "e", "f", "g"])
    d2 = pd.Series (["a", "c", "d", "g"])
    d1_s = set (d1)
    d2_s = set (d2)
    A = d1_s&d2_s
    for word in sorted (d1_s | d2_s):
        if word in A:
            print (word, "O")
        else:
            print (word, "X")
    "" "=>
    a O
    b X
    c O
    d O
    e X
    f X
    g O
    "" "

  • Answer # 2

    I expressed a group of words with a simple dict without using pandas, and wrote it.

    import collections
    ws = [
        ['a', 'b', 'c', 'd', 'e', ​​'f', 'g'],
        ['a', 'c', 'd', 'g']
    ]
    wall = ws [0] + ws [1]
    print (wall)
    c = collections.Counter (wall)
    print (c)
    result = {1: "x", 2: "o"}
    c2 = dict ([(k, result [v]) for k, v in c.items ()])
    print (c2)
    for k, v in c2.items ():
        print (k, "->", v)

    Execution result