python - Pandas reformatting multiple columns into one. -
i have code generates pandas dataframe of dependencies:
input | output | script
i trying generate list of distinct values matrix new table 1 column.
nodes
i tried
nodes_list = pd.dataframe({nodes: [dependency['input'].values, dependency['output'].values, dependency['script'].values]})
but rather getting 3 columns merged 3 row dataframe values comma separated values inside? how can append 3 columns onto each other distinct values elegantly?
thanks
say dataframe like
in [295]: df out[295]: input output script 0 aaa bbb ggg 1 ddd hhh ccc 2 eee bbb fff 3 aaa bbb kkk
you can flatten using ravel()
, take unique
values
in [296]: np.unique(df.values.ravel()) out[296]: array(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg', 'hhh', 'kkk'], dtype=object)
or using np.unique()
directly on df
gives same output
in [301]: np.unique(df) out[301]: array(['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg', 'hhh', 'kkk'], dtype=object)
and, can create nodes_list
with
in [297]: pd.dataframe({'nodes': np.unique(df)}) out[297]: nodes 0 aaa 1 bbb 2 ccc 3 ddd 4 eee 5 fff 6 ggg 7 hhh 8 kkk
Comments
Post a Comment