I have two dataframes, like below
rows_df
Id Location Age
0 0 30
1 1 US 20
2 2
requiredCols_df
RequiredColumn
0 Location
requiredCols_df specifies which column is required in rows_df.
In this example, Location is required, Age is optional in rows_df.
I want to filter the rows_df based on the requiredCols_df. So there will be two resulting dataframes. One contains rows that have the required columns, and the other dataframe contains rows that don't have any required columns.
Expected Result
Rows matched
Id Location Age
1 1 US 20
Rows don't match
Id Location Age
0 0 30
2 2
Please note that:
1.The rows_df contains more than two columns, e.g. 10-30 columns.
2 The requiredCols_df contains more than one row.
3 Please note that Location contains a ' ' (empty space(s)) in row 0, and a null (empty) in row 2.
rows_df = pd.DataFrame({'Id':['0','1','2'],
'Location': [' ', 'US', None],
'Age':['30','20','']})
column names below specify which column in rows_df must be not empty
requiredCols_df = pd.DataFrame([['Location']],
columns= ['RequiredColumn'])
4 Update: Both the resulting DataFrame contain the original non-changed values.
I can do this with a loop, but I want to see if there is a better solution.