python - Do Column Headers Exist in Row of Dataframe? -


here example dataframe:

cols = ["report_suite", "productid", "manufacturer", "brand manager", "finish"] data = [["rs_1", "productid", "manufacturer", "finish", np.nan], ["rs_2",  "productid", "manufacturer", "brand manager", "finish"], ["rs_3",  "brand manager", "finish", np.nan, np.nan]] df = pd.dataframe(data, columns = cols) 

what want have pivot table boolean in each column whether column header in row of data (not including report_suite column). final output want this:

cols = ["report_suite", "productid", "manufacturer", "brand manager", "finish"] data = [["rs_1", 1, 1, 0, 1], ["rs_2", 1, 1, 1, 1], ["rs_3",  0, 0, 1, 1]] final_df = pd.dataframe(data, columns = cols) 

in [185]: df.set_index('report_suite') \             .apply(lambda x: x.eq(x.name)) \             .astype(np.int8) \             .reset_index() out[185]:   report_suite  productid  manufacturer  brand manager  finish 0         rs_1          1             1              0       0 1         rs_2          1             1              1       1 2         rs_3          0             0              0       0 

or

in [191]: df.set_index('report_suite') \             .fillna('') \             .apply(lambda x: x.str.contains(x.name)) \             .astype(np.int8) \             .reset_index() out[191]:   report_suite  productid  manufacturer  brand manager  finish 0         rs_1          1             1              0       0 1         rs_2          1             1              1       1 2         rs_3          0             0              0       0 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -