How to reorder array in Python with / without Pandas? -
in order search correlations between products , categories , next visualizations (heatmaps) need reorder array using python with/without pandas or other libraries this:
book name, category 1, category 2, category 3, django 101 python web-dev beginner ror guide rails web-dev intermediate laravel php web-dev intermediate
into that:
book name, python, web-dev, beginner, rails, php, intermediate django 101 true true true false false, false ror guide false true false false false, true laravel false true false false true, true
is there way that? data stored .csv file , read pandas.read_csv ()
this can done using get_dummies
function in pandas.
df = pd.dataframe({'book name': ['django 101', 'ror guide', 'laravel'], 'category 1': ['python', 'rails', 'php'], 'category 2': ['web-dev']*3, 'category 3': ['beginner', 'intermediate', 'intermediate']}) dummies = pd.concat([pd.get_dummies(df[c]) c in df.columns[1:]], axis=1) df_new = pd.concat([df['book name'], dummies], axis=1) >>> df_new book name php python rails web-dev beginner intermediate 0 django 101 0 1 0 1 1 0 1 ror guide 0 0 1 1 0 1 2 laravel 1 0 0 1 0 1
or can reset index of dataframe book's name:
df.set_index('book name', inplace=true) df_new = pd.concat([pd.get_dummies(df[c]) c in df], axis=1) >>> df_new php python rails web-dev beginner intermediate book name django 101 0 1 0 1 1 0 ror guide 0 0 1 1 0 1 laravel 1 0 0 1 0 1
Comments
Post a Comment