0

I have two different dfs with the following columns:

col1                    col2

0 programming           0 programming
1 chess                 1 python
2 leadership            2 leadership
                        3 abba
                        4 games

I want to find what percentage of values of col1 present in col2

For that, I thought I could construct a new df which will contain the same values and then its len to len(col2). First I want to get this:

col3

0 programming
1 leadership 

Here is what I tried, but I dont want to use two for loops

bb=[]
for i in company.col2:
    for b in student.col1:
        if i==b:
            bb.append(i)

3 Answers 3

1
>>> df1 = pd.DataFrame(["programming", "chess", "leadership"], columns=["col1"])
>>> df2 = pd.DataFrame(["programming", "python", "leadership", "abba", "games"], columns=["col2"])

To find which values of df1['col1'] are in df2['col2'] use isin. The result is a boolean column. Use sum to find the number of values:

>>> df1['col1'].isin(df2['col2']).sum()

Use size to find the number of elements in df2 and divide thus:

>>> df1['col1'].isin(df2['col2']).sum() / df2.size
Sign up to request clarification or add additional context in comments.

Comments

0

You can use list comprehension, try the below

list1 = ["programming", "chess", "leadership"]
list2 = ["programming", "python", "leadership", "abba", "games"]
list3 = [value for value in list1 if value in list2] 

list3 = ["programming", "leadership"]

In your use case, you can just try the below.

list3 = [value for value in company.col2 if value in student.col1]  

Comments

0

You can use pandas.merge to perform an inner join:

def main():

    df = pd.DataFrame(["programming", "chess", "leadership"], columns=["col1"])
    df2 = pd.DataFrame(["programming", "python", "leadership", "abba", "games"], columns=["col2"])
    df3 = df.merge(df2, left_on="col1", right_on="col2", how="inner")
    print(df3["col1"])

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.