2

I have two python lists in following form:

A = [(1,''), (1, 'ABC'),(1,''), (1, 'DEF'),(1,''), (1, 'GHI'),(1,''), (1, 'LMO'),(1,'')]
B = ['ABC', 'ghi', 'PQR']

(Note: A is a list of list. B is normal list.)

I want to create a pandas DF that will only contain element that are common from both the lists and the form will be:

DF :
A     B
ABC   ABC
GHI   ghi 

Please note there might be lower case- upper case and also white spaces

5 Answers 5

1

you can try:

import numpy as np
a = np.intersect1d(np.array(A)[:,1], np.array(B))
df = pd.DataFrame({'A': a, 'B': a})
Sign up to request clarification or add additional context in comments.

Comments

1

Try:

t = [v for _, v in A if v in B]
df = pd.DataFrame({"A": t, "B": t})
print(df)

Prints:

     A    B
0  ABC  ABC
1  GHI  GHI

To handle upper/lower cases:

B = [v.upper() for v in B]
t = [v.upper() for _, v in A if v.upper() in B]
df = pd.DataFrame({"A": t, "B": t})
print(df)

2 Comments

Will this handle lower case upper case as well? plus white spaces?
A slight modification of your answer did the work. thanks a lot.
1

Input data:

>>> dfA
   _      A
0  1
1  1    AbC
2  1
3  1    DEF
4  1
5  1  G H I
6  1
7  1    LMO
8  1

>>> dfB
     B
0  ABC
1  GHI
2  PQR
>>> pd.merge(dfA, dfB,
             left_on=dfA["A"].str.upper().str.replace(' ', ''),
             right_on=dfB["B"].str.upper().str.replace(' ', ''))[["A", "B"]]
       A    B
0    AbC  ABC
1  G H I  GHI

2 Comments

Will this handle lower case , upper case and white spaces?
@KathanVyas. Now yes :-)
1
import pandas as pd

A = [(1,''), (1, 'ABC'),(1,''), (1, 'DEF'),(1,''), (1, 'GHI'),(1,''), (1, 'LMO'),(1,'')]
B = ['ABC', 'GHI', 'PQR']

A = [i[1] for i in A if i[1] in B]
df = pd.DataFrame({'A':A,'B':A})
print(df)

to take care about case sensitivity and spaces try this:

import pandas as pd

A = [(1,''), (1, 'ABC'),(1,''), (1, 'DEF'),(1,''), (1, 'GHI'),(1,''), (1, 'LMO'),(1,'')]
B = ['ABC', 'GHI', 'PQR']

A = [i[1] for i in A if i[1].lower().replace(' ', '') in [x.lower().replace(' ','') for x in B]]
df = pd.DataFrame({'A': A, 'B': A})
 print(df)

4 Comments

Will this handle lower case , upper case and white spaces?
can you add more details to get more contexts out of the answer you suggesting?
@KathanVyas yes now it does take care about all. I have updated the ans
@LinhNguyen sure. iterating pandas series A, and picking the element if and only if the first element of the iterating element is present in list B later case sensitivity and spaces is also take care off as asked by Kathan
0

Try:

t = [v for _, v in A if v in B]
df = pd.DataFrame({"A": t, "B": t})
print(df)

Prints:

     A    B
0  ABC  ABC
1  GHI  GHI

To handle upper/lower cases:

t = [v.upper() or v.lower() for _, v in A if v.upper() or or v.lower() in B]
df = pd.DataFrame({"A": t, "B": t})
print(df)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.