I have the following dataframe
JavaScript
x
9
1
A B
2
0 1 1
3
1 1 2
4
2 1 3
5
0 2 1
6
1 2 2
7
2 2 3
8
9
And I would like to check if the dataframe is a complete combination of the entries in each column. In the above dataframe this is the case. A = {1,2} B = {1,2,3} and the dataframe contains all possible combinations. Following example would result in a false.
JavaScript
1
6
1
A B
2
0 1 1
3
1 1 2
4
0 2 1
5
6
The number of columns should be flexible.
Many thanks for your help!
Advertisement
Answer
JavaScript
1
3
1
df = pd.DataFrame({'A': [1,1,1,2,2,2],
2
'B': [1,2,3,1,2,3]})
3
Create a data frame with all combinations of unique values in all columns
JavaScript
1
12
12
1
uniques = [df[i].unique().tolist() for i in df.columns]
2
df_combo = pd.DataFrame(product(*uniques), columns = df.columns)
3
print(df_combo)
4
5
A B
6
0 1 1
7
1 1 2
8
2 1 3
9
3 2 1
10
4 2 2
11
5 2 3
12
Test if two dataframes contain the same elements
JavaScript
1
3
1
df.equals(df_combo)
2
True
3
For False scenario,
JavaScript
1
13
13
1
df = pd.DataFrame({'A': [1,1,2],
2
'B': [1,2,1]})
3
4
df_combo
5
A B
6
0 1 1
7
1 1 2
8
2 2 1
9
3 2 2
10
11
df.equals(df_combo)
12
False
13