How to do group by on a multiindex in pandas?

You can create the index on the existing dataframe. With the subset of data provided, this works for me: import pandas df = pandas.DataFrame.from_dict( { ‘category’: {0: ‘Love’, 1: ‘Love’, 2: ‘Fashion’, 3: ‘Fashion’, 4: ‘Hair’, 5: ‘Movies’, 6: ‘Movies’, 7: ‘Health’, 8: ‘Health’, 9: ‘Celebs’, 10: ‘Celebs’, 11: ‘Travel’, 12: ‘Weightloss’, 13: ‘Diet’, 14: … Read more

Creating an empty MultiIndex

The solution is to leave out the labels. This works fine for me: >>> import pandas as pd >>> my_index = pd.MultiIndex(levels=[[],[],[]], … codes=[[],[],[]], … names=[u’one’, u’two’, u’three’]) >>> my_index MultiIndex([], names=[‘one’, ‘two’, ‘three’]) >>> my_columns = [u’alpha’, u’beta’] >>> df = pd.DataFrame(index=my_index, columns=my_columns) >>> df Empty DataFrame Columns: [alpha, beta] Index: [] >>> df.loc[(‘apple’,’banana’,’cherry’),:] … Read more

Set MultiIndex of an existing DataFrame in pandas

When you pass inplace in makes the changes on the original variable and returns None, and the function does not return the modified dataframe, it returns None. is_none = df.set_index([‘Company’, ‘date’], inplace=True) df # the dataframe you want is_none # has the value None so when you have a line like: df = df.set_index([‘Company’, ‘date’], … Read more

Concatenate Pandas columns under new multi-index level

You can do it with concat (the keys argument will create the hierarchical columns index): d = {‘ABC’ : df1, ‘XYZ’ : df2} print pd.concat(d.values(), axis=1, keys=d.keys()) XYZ ABC \ Open High Low Close Volume Open High Date 2002-01-17 0.18077 0.18800 0.16993 0.18439 1720833 0.18077 0.18800 2002-01-18 0.18439 0.21331 0.18077 0.19523 2027866 0.18439 0.21331 2002-01-21 … Read more

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