-2

thanks for your help. I can not find a solution to a problem which should be really easy to solve:

Starting with a DataFrame like this:

print(df)
       Column A  Column B  Column C
Row 1         1         3         3
Row 2         2         4         6
Row 3         3         5         7
Row 4       nan         3       nan

I am trying to find values which can be found in every column. In this example, that would be value "3" because it can be found in columns A to C.

Thanks in advance for your help! I am just not able to find a solution.

User5644
  • 5
  • 3
  • ``df.loc[df.ColumnA.isin(df.ColumnB) & df.ColumnA.isin(df.ColumnC), 'ColumnA'].item()``. It uses columnA to check in every other column, filters the original dataframe with the boolean, to get the output – sammywemmy Aug 10 '21 at 13:48
  • Can you share expected output, [Does this solve your problem ?](https://stackoverflow.com/questions/26640129/search-for-string-in-all-pandas-dataframe-columns-and-filter) – Ibrahim Ayoup Aug 10 '21 at 13:56

2 Answers2

0

Use set to find intersection of each column:

>>> set.intersection(*df.astype(float).apply(set))
{3.0}
Corralien
  • 109,409
  • 8
  • 28
  • 52
0

Thanks for your help. Unfortunately I wasn't able to use your responses successfully.

At the end, I will implement it like this:

df1 = pd.DataFrame([2,1,3])
df1.columns = ['column']
df2 = pd.DataFrame([1,5,3])
df2.columns = ['column']
df3 = pd.DataFrame([1,1,9])
df3.columns = ['column']
df4 = pd.DataFrame([1,10,9])
df4.columns = ['column']


df12 = df1.loc[df1["column"].isin(df2["column"]), "column"]
df12 = pd.DataFrame(df12)
df34 = df3.loc[df3["column"].isin(df4["column"]), "column"]
df34 = pd.DataFrame(df34)
df_final = df12.loc[df12["column"].isin(df34["column"]), "column"]
User5644
  • 5
  • 3
  • Can you explain me why you can use my solution? My answer seems to solve your problem... You want to find the common values across columns which is 3 in you sample. – Corralien Aug 10 '21 at 18:30