python - Row and column indexes of True values -
i have nxn pandas dataframe contains booleans. example:
in[56]: df out[56]: 15 25 35 45 55 10 true false false false false 20 false true false false false 30 false false true false false 40 false false false true false 50 false false false false true
what need collapse frame nx2 pandas dataframe has index , column values have true
@ intersection record values. example:
in[62]: res out[62]: 0 1 0 10 15 1 20 25 2 30 35 3 40 45 4 50 55
can't seem find simple way this.
pd.melt
"unpivots" dataframe wide long format:
result = pd.melt(df.reset_index(), id_vars=['index']) mask = result['value'] == true result = result.loc[mask, ['index', 'variable']] result.columns = [0, 1] print(result)
yields
0 1 0 10 15 6 20 25 12 30 35 18 40 45 24 50 55
ps. desired dataframe has 2 columns have values act coordinates. df.pivot
method converts dataframes coordinate-like columns "wide-format" dataframes original dataframe. when want go other way, use pd.melt
.
Comments
Post a Comment