Create multiindex from existing dataframe

You could simply use groupby in this case, which will create the multi-index automatically when it sums the sales along the requested columns. df.groupby([‘user_id’, ‘account_num’, ‘dates’]).sales.sum().to_frame() You should also be able to simply do this: df.set_index([‘user_id’, ‘account_num’, ‘dates’]) Although you probably want to avoid any duplicates (e.g. two or more rows with identical user_id, account_num … Read more

Sort dataframe by string length

You can use reindex of index of Series created by len with sort_values: print (df.name.str.len()) 0 5 1 2 2 6 3 4 Name: name, dtype: int64 print (df.name.str.len().sort_values()) 1 2 3 4 0 5 2 6 Name: name, dtype: int64 s = df.name.str.len().sort_values().index print (s) Int64Index([1, 3, 0, 2], dtype=”int64″) print (df.reindex(s)) name score … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)