Skip to content Skip to sidebar Skip to footer

Python Dataframe Include Common Points In Lists

I have a dataframe made of lists. Two columns: x and y. I want to include common points in x and corresponding points in y. My code: df = x

Solution 1:

Here is my suggestion:

from collections import Counter

temp= df.apply(pd.Series.explode)

l=Counter(temp.x)

s=[x for x, count in l.items() if count==df.shape[0]]

res = temp[temp.x.isin(s)]

res = res.groupby(level=0).agg(list)

>>>print(res)   
              x              y
0  [0, 1.1, 10]  [0, 1.5, 4.5]
1  [0, 1.1, 10]  [0, 100, 400]
2  [0, 1.1, 10]  [0, 300, 900]

Let me know if you need some explanation about how it works.

Post a Comment for "Python Dataframe Include Common Points In Lists"