<p>Here  you go, just add column 'C' and you are sorted. Just to note, you must be seeking a <code>pandas</code> solution because <code>merge</code> is a <code>pandas</code> command and you're asking for a <code>Python</code> solution.</p>
<pre><code>import pandas as pd

a = [&quot;house&quot;,&quot;garden&quot;, &quot;living room&quot;, &quot;dog&quot;,&quot;cat&quot;]
b= [&quot;cat&quot;,&quot;dog&quot;, &quot;chicken&quot;]
df = pd.DataFrame(a, columns = ['a'])
df2 = pd.DataFrame(b, columns = ['b'])
dfa = df['a'].value_counts()
dfa.columns = ['a']
dfb = df2['b'].value_counts()
dfb.columns = ['b']
dfa = dfa.to_frame()
dfb = dfb.to_frame()
df3 = dfa.join(dfb).replace(np.nan, 0).astype(int)
print (df3)
</code></pre>
<p>Output</p>
<pre><code>             a  b
house        1  0
garden       1  0
living room  1  0
dog          1  1
cat          1  1
</code></pre>
<p>and if you want to remove the 'a' column</p>
<pre><code>print (df3.drop('a', axis=1))

             b
house        0
garden       0
living room  0
dog          1
cat          1
</code></pre>
<hr />
<p><strong>Notes</strong> The key command here is <code>value_counts</code> and makes a frequency plot. Its output is a <code>Series</code> rather than a <code>DataFrame</code> so it needs converting.</p>
<p>If you want to keep all inputs the commands is</p>
<pre><code>df3 = pd.concat([dfa, dfb]).replace(np.nan, 0).astype(int)
</code></pre>
<hr />
<p>Alternatively,</p>
<pre><code>a = [&quot;house&quot;,&quot;garden&quot;, &quot;living room&quot;, &quot;dog&quot;,&quot;cat&quot;]
b= [&quot;cat&quot;,&quot;dog&quot;, &quot;chicken&quot;]
df = pd.DataFrame(a).value_counts().to_frame('a')
df2 = pd.DataFrame(b).value_counts().to_frame('b')
df3 = df.join(df2).replace(np.nan, 0).astype(int).rename_axis(None)
print (df3.drop('a', axis=1))
</code></pre>

CC BY-SA 4.0
2022-09-13T13:19:04.133Z
2022-09-13T19:24:08.713Z
2022-09-13T19:24:08.713Z
M__
<p>I'm aware that this asks specifically for a python solution, but I'd like to add an R answer as well, just in case someone using R has a similar problem:</p>
<pre><code>&gt; a &lt;- c(&quot;house&quot;,&quot;garden&quot;, &quot;living room&quot;, &quot;dog&quot;,&quot;cat&quot;)
&gt; b &lt;- c(&quot;cat&quot;,&quot;dog&quot;, &quot;chicken&quot;)
&gt; c &lt;- c(&quot;house&quot;, &quot;garden&quot;,&quot;bathroom&quot;)

&gt; (result &lt;- data.frame(row.names=a, a=a %in% a, b=a %in% b, c=a %in% c))
               a     b     c
house       TRUE FALSE  TRUE
garden      TRUE FALSE  TRUE
living room TRUE FALSE FALSE
dog         TRUE  TRUE FALSE
cat         TRUE  TRUE FALSE
</code></pre>
<p>or for strictly what you asked for, the TRUE/FALSE values can be encouraged into numbers by adding zero:</p>
<pre><code>&gt; (result &lt;- data.frame(a=a, b=(a %in% b) + 0, c=(a %in% c) + 0))
            a b c
1       house 0 1
2      garden 0 1
3 living room 0 0
4         dog 1 0
5         cat 1 0
</code></pre>

CC BY-SA 4.0
2022-09-14T21:49:42.547Z
2022-09-14T21:49:42.547Z
gringer
<p>Both existing answers seem to work to my eye. An alternative approach:</p>
<pre><code>import pandas as pd

a = [&quot;house&quot;,&quot;garden&quot;, &quot;living room&quot;, &quot;dog&quot;,&quot;cat&quot;]
b = [&quot;cat&quot;,&quot;dog&quot;, &quot;chicken&quot;]
c = [&quot;house&quot;, &quot;garden&quot;,&quot;bathroom&quot;, &quot;unique&quot;]
</code></pre>
<p>First we generate all options available. This way your 'a' list is not restricting the options. We use <a href="https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset" rel="nofollow noreferrer">sets</a> to get unique values.</p>
<pre><code>options = list(set(a + b + c))
wanted = pd.DataFrame({'options' : options} )
</code></pre>
<p>Next we loop over all the lists. We use <a href="https://docs.python.org/3/tutorial/datastructures.html" rel="nofollow noreferrer">tuples</a> to assign both name and variable.</p>
<pre><code>for name, _ in [('a',a),('b',b),('c',c)]:
    wanted.loc[wanted['options'].isin(_), name ] = 1
</code></pre>
<p>Finally we fill the missing values with zeroes.</p>
<pre><code>wanted = wanted.fillna('0')
</code></pre>
<p>In case you have large number of lists, you can iterate over ascii_lowercase letters:</p>
<pre><code>my_tuples = ()
from string import ascii_lowercase

for _ in ascii_lowercase[:3]: #&lt;-magic number for number of lists here
     my_tuples = my_tuples + (str(_), eval(_))
</code></pre>
<p>Then change for loop as:</p>
<pre><code>for name, _ in my_tuples:
     ....
</code></pre>

CC BY-SA 4.0
2023-10-10T07:11:31.453Z
2023-10-10T07:51:13.817Z
2023-10-10T07:51:13.817Z
pinegulf
<p>I have multiple lists on python:</p>
<pre><code>a = [&quot;house&quot;,&quot;garden&quot;, &quot;living room&quot;, &quot;dog&quot;,&quot;cat&quot;]

b= [&quot;cat&quot;,&quot;dog&quot;, &quot;chicken&quot;]

c=[&quot;house&quot;, &quot;garden&quot;,&quot;bathroom&quot;]
</code></pre>
<p>I'd like to create the following dataframe:</p>
<div class="s-table-container">
<table class="s-table">
<thead>
<tr>
<th style="text-align: left;">a</th>
<th style="text-align: center;">b</th>
<th style="text-align: right;">c</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: left;">house</td>
<td style="text-align: center;">0</td>
<td style="text-align: right;">1</td>
</tr>
<tr>
<td style="text-align: left;">garden</td>
<td style="text-align: center;">0</td>
<td style="text-align: right;">1</td>
</tr>
<tr>
<td style="text-align: left;">living room</td>
<td style="text-align: center;">0</td>
<td style="text-align: right;">0</td>
</tr>
<tr>
<td style="text-align: left;">dog</td>
<td style="text-align: center;">1</td>
<td style="text-align: right;">0</td>
</tr>
<tr>
<td style="text-align: left;">cat</td>
<td style="text-align: center;">1</td>
<td style="text-align: right;">0</td>
</tr>
</tbody>
</table>
</div>
<p>Thanks for the help!</p>

CC BY-SA 4.0
2022-09-13T13:31:13.573Z
Hi @Marco if you do find the answer below okay (it does work), could you very kindly mark the post as 'accepted'. There's a little grey 'tick' sign to the left of the answer, click that and it goes green. I noted on your past questions you forgotten to do that. Its a small thing but helps everyone, which includes you (you will get +2).
M__
<python><phylogenetics><merge><pandas><dataframe>
How to merge multiple list into a single dataframe replacing common values with 1 -python
# How to merge multiple list into a single dataframe replacing common values with 1 -python

(Asked by: username_0 on 2022-09-13)

I have multiple lists on python:
a = ["house","garden", "living room", "dog","cat"]

b= ["cat","dog", "chicken"]

c=["house", "garden","bathroom"]

I'd like to create the following dataframe:




a
b
c




house
0
1


garden
0
1


living room
0
0


dog
1
0


cat
1
0




Thanks for the help!

**Tags:** python, phylogenetics, merge, pandas, dataframe
> **Comments:**
> **username_1**: Hi @Marco if you do find the answer below okay (it does work), could you very kindly mark the post as 'accepted'. There's a little grey 'tick' sign to the left of the answer, click that and it goes green. I noted on your past questions you forgotten to do that. Its a small thing but helps everyone, which includes you (you will get +2).  

---

## 3 answers

### Answer by username_1 on 2022-09-13. Score: 1

Here  you go, just add column 'C' and you are sorted. Just to note, you must be seeking a pandas solution because merge is a pandas command and you're asking for a Python solution.
import pandas as pd

a = ["house","garden", "living room", "dog","cat"]
b= ["cat","dog", "chicken"]
df = pd.DataFrame(a, columns = ['a'])
df2 = pd.DataFrame(b, columns = ['b'])
dfa = df['a'].value_counts()
dfa.columns = ['a']
dfb = df2['b'].value_counts()
dfb.columns = ['b']
dfa = dfa.to_frame()
dfb = dfb.to_frame()
df3 = dfa.join(dfb).replace(np.nan, 0).astype(int)
print (df3)

Output
             a  b
house        1  0
garden       1  0
living room  1  0
dog          1  1
cat          1  1

and if you want to remove the 'a' column
print (df3.drop('a', axis=1))

             b
house        0
garden       0
living room  0
dog          1
cat          1


Notes The key command here is value_counts and makes a frequency plot. Its output is a Series rather than a DataFrame so it needs converting.
If you want to keep all inputs the commands is
df3 = pd.concat([dfa, dfb]).replace(np.nan, 0).astype(int)


Alternatively,
a = ["house","garden", "living room", "dog","cat"]
b= ["cat","dog", "chicken"]
df = pd.DataFrame(a).value_counts().to_frame('a')
df2 = pd.DataFrame(b).value_counts().to_frame('b')
df3 = df.join(df2).replace(np.nan, 0).astype(int).rename_axis(None)
print (df3.drop('a', axis=1))


---

### Answer by username_2 on 2022-09-14. Score: 1

I'm aware that this asks specifically for a python solution, but I'd like to add an R answer as well, just in case someone using R has a similar problem:
> a <- c("house","garden", "living room", "dog","cat")
> b <- c("cat","dog", "chicken")
> c <- c("house", "garden","bathroom")

> (result <- data.frame(row.names=a, a=a %in% a, b=a %in% b, c=a %in% c))
               a     b     c
house       TRUE FALSE  TRUE
garden      TRUE FALSE  TRUE
living room TRUE FALSE FALSE
dog         TRUE  TRUE FALSE
cat         TRUE  TRUE FALSE

or for strictly what you asked for, the TRUE/FALSE values can be encouraged into numbers by adding zero:
> (result <- data.frame(a=a, b=(a %in% b) + 0, c=(a %in% c) + 0))
            a b c
1       house 0 1
2      garden 0 1
3 living room 0 0
4         dog 1 0
5         cat 1 0


---

### Answer by username_3 on 2023-10-10. Score: 1

Both existing answers seem to work to my eye. An alternative approach:
import pandas as pd

a = ["house","garden", "living room", "dog","cat"]
b = ["cat","dog", "chicken"]
c = ["house", "garden","bathroom", "unique"]

First we generate all options available. This way your 'a' list is not restricting the options. We use sets to get unique values.
options = list(set(a + b + c))
wanted = pd.DataFrame({'options' : options} )

Next we loop over all the lists. We use tuples to assign both name and variable.
for name, _ in [('a',a),('b',b),('c',c)]:
    wanted.loc[wanted['options'].isin(_), name ] = 1

Finally we fill the missing values with zeroes.
wanted = wanted.fillna('0')

In case you have large number of lists, you can iterate over ascii_lowercase letters:
my_tuples = ()
from string import ascii_lowercase

for _ in ascii_lowercase[:3]: #<-magic number for number of lists here
     my_tuples = my_tuples + (str(_), eval(_))

Then change for loop as:
for name, _ in my_tuples:
     ....


---

