JavaScript
x
34
34
1
df1 = pd.DataFrame(
2
{
3
"empid" : [1,2,3,4,5,6],
4
"empname" : ['a', 'b','c','d','e','f'],
5
"empcity" : ['aa','bb','cc','dd','ee','ff']
6
})
7
df1
8
9
df2 = pd.DataFrame(
10
{
11
"empid" : [1,2,3,4,5,6],
12
"empname" : ['a', 'b','m','d','n','f'],
13
"empcity" : ['aa','bb','cc','ddd','ee','fff']
14
})
15
df2
16
17
df_all = pd.concat([df1.set_index('empid'),df2.set_index('empid')],axis='columns',keys=['first','second'])
18
df_all
19
20
df_final = df_all.swaplevel(axis = 'columns')[df1.columns[1:]]
21
df_final
22
23
orig = df1.columns[1:].tolist()
24
print (orig)
25
['empname', 'empcity']
26
27
df_final = (df_all.stack()
28
.assign(comparions=lambda x: x['first'].eq(x['second']))
29
.unstack()
30
.swaplevel(axis = 'columns')
31
.reindex(orig, axis=1, level=0))
32
print (df_final)
33
34
How to filter level[0] column name list where comparions = False from the dataframe df_final(consider there are more than 300 column like this at level 0)
Advertisement
Answer
First test if in level comparions
are all True
s by DataFrame.xs
with DataFrame.all
:
JavaScript
1
2
1
s = df_final.xs('comparions', level=1, axis=1).all()
2
And then invert mask for test at least one False
with filter indices:
JavaScript
1
4
1
L = s.index[~s].tolist()
2
print (L)
3
['empname', 'empcity']
4