Reputation: 71
Suppose I have a dataframe
column1 column2 column3
1 2 4
2 3 5
3 4 6
4 5 7
5 6 8
How would I able to find the values appear among these three columns?
Like in the above dataframe the output will be [4,5]
I tried the code
for index, row in df.iterrows():
for i in row["column1"]:
for j in row["column2"]:
if row["column1"][i] == row["column2"][j]:
print row["column1"][i]
and it jumps out the error
TypeError: 'numpy.float64' object is not iterable
I also tried
for i in df["column1"].iterrows():
and the error is
AttributeError: 'numpy.float64' object has no attribute 'iterrows'
Upvotes: 2
Views: 63
Reputation: 323226
Try this:
s=df.melt().groupby('value').variable.nunique().loc[lambda x : x==df.shape[1]]
value
4 3
5 3
Name: variable, dtype: int64
#s.index.tolist()
Upvotes: 3
Reputation: 38415
You can use numpy intersect1d,
from functools import reduce
reduce(np.intersect1d, list(df.to_numpy().T))
Output: array([4, 5])
Upvotes: 2