Reputation: 721
sorted(X_train_num.F12.unique())==sorted(X_test_num.F12.unique())
basically I want to run loop over different variable like F12,F11,F78 etc. so that I can avoid writing the single command again and again for different variable.
Does someone knows simple way to loop over this statement with different var(F12,F11, etc)
Upvotes: 1
Views: 63
Reputation: 866
Side note: you can also do the comparison more efficiently by using set equality:
set(getattr(X_train_num, var)) == set(getattr(X_test_num, var))
Upvotes: 1
Reputation: 113864
Use getattr
:
var = 'F12'
sorted(getattr(X_train_num, var).unique())==sorted(getattr(X_test_num, var).unique())
The above can be easily put in a loop. For example:
for var in ('F10', 'F11', 'F12'):
sorted(getattr(X_train_num, var).unique())==sorted(getattr(X_test_num, var).unique())
Upvotes: 4