Is it possible to pass a column list and check if it exists in df.columns, then keep those column and drop others ?
col_list = ['Name of Company','Incorporated','Country']
Example – I have a dataframe df1 like below –
|Name of Company|Incorporated|Ownership|
|American Money Management Corporation|Ohio|100|
|APU Holding Company|Ohio|100|
|American Premier Underwriters, Inc.|Pennsylvania|100|
Another dataframe df2 like below-
|Name|Country|Nature of business|Shares held by the Group|Shares held by NCI|
|Directly held interests|NaN|NaN|NaN|NaN|
|Ferrari S.p.A.|Italy|Manufacturing|100%|-%|
|Indirectly held through Ferrari S.p.A.|NaN|NaN|NaN|NaN|
The column list passed above is for both the dataframe I need those columns only if it exists and drop rest and return the data for both dataframes in json format using json.dumps().
Expected output for df1-
{"Name of Company":"American Money Management Corporation","Incorporated":"Ohio"}
{"Name of Company":"APU Holding Company","Incorporated":"Ohio"}
{"Name of Company":"American Premier Underwriters, Inc.","Incorporated":"Pennsylvania"}
Expected output for df2, remove the field having NaN values from json –
{"Name":"Directly held interests"}
{"Name":"Ferrari S.p.A.","Country":"Italy"}
{"Name of Company":"Indirectly held through Ferrari S.p.A."}
Any help would be appreciated.