Hope you all doing well
I want to find the most common elements in a DataFrame the elements that appear in all rows
I have this function , but it's work only when the DataFrame have two rows.
def find_common_elements(df):
df = df.drop(['motif', 'frequency', 'motif_cleaned'], axis=1)
df = df.dropna(axis=1, how='all')
# Get the set of elements in the first row
common_elements = set(df.iloc[0])
# Iterate over the remaining rows and update the common elements
for i in range(1, len(df)):
common_elements.intersection_update(set(df.iloc[i]))
return common_elements
Please anyone can help
For example if i have this data # Sample data
data = {
'A': [1, 1, 1, 1, 1],
'B': [2, 1, 2, 2, 1],
'C': [1, 2, 0, 0, 2]
}
the most common elements here 1 and 2 that appear in all rows
Thank you !