How to make two rows in a pandas dataframe into column headers

If using pandas.read_csv() or pandas.read_table(), you can provide a list of indices for the header argument, to specify the rows you want to use for column headers. Python will generate the pandas.MultiIndex for you in df.columns: df = pandas.read_csv(‘DollarUnitSales.csv’, header=[0,1]) You can also use more than two rows, or non-consecutive rows, to specify the column … Read more

Filtering multiple items in a multi-index Pandas dataframe

You can get_level_values in conjunction with Boolean slicing. In [50]: print df[np.in1d(df.index.get_level_values(1), [‘Lake’, ‘River’, ‘Upland’])] Area NSRCODE PBL_AWI CM Lake 57124.819333 River 1603.906642 LBH Lake 258046.508310 River 44262.807900 The same idea can be expressed in many different ways, such as df[df.index.get_level_values(‘PBL_AWI’).isin([‘Lake’, ‘River’, ‘Upland’])] Note that you have ‘upland’ in your data instead of ‘Upland’

Converting a Pandas GroupBy multiindex output from Series back to DataFrame

g1 here is a DataFrame. It has a hierarchical index, though: In [19]: type(g1) Out[19]: pandas.core.frame.DataFrame In [20]: g1.index Out[20]: MultiIndex([(‘Alice’, ‘Seattle’), (‘Bob’, ‘Seattle’), (‘Mallory’, ‘Portland’), (‘Mallory’, ‘Seattle’)], dtype=object) Perhaps you want something like this? In [21]: g1.add_suffix(‘_Count’).reset_index() Out[21]: Name City City_Count Name_Count 0 Alice Seattle 1 1 1 Bob Seattle 2 2 2 Mallory … Read more

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

Read multi-index on the columns from csv file

I think the problem is that you have duplicated columns: two ( Female, R). Not sure whether it’s a bug or the duplicated columns are unacceptable. Here’s a workaround for you: First read the csv with tupleize_cols=True In [61]: df = pd.read_csv(‘test.csv’, header=[0, 1], skipinitialspace=True, tupleize_cols=True) In [62]: df Out[62]: (Male, R) (Male, R) (Male, … Read more

How to reindex a MultiIndex dataframe

To get the B using reindex B.reindex( pd.MultiIndex.from_product([B.index.levels[0], A.index], names=[‘Bank’, ‘Curency’]),fill_value=0) Out[62]: Notional Bank Curency Bank_1 AUD 16 BRL 0 CAD 13 EUR 22 INR 0 Bank_2 AUD 24 BRL 0 CAD 20 EUR 17 INR 0 To get the A using concat pd.concat([A]*2,keys=B.index.levels[0]) Out[69]: AUD BRL CAD EUR INR Bank Bank_1 AUD 10 5 … Read more

What are levels in a pandas DataFrame?

I stumbled across this question while analyzing the answer to my own question, but I didn’t find the John’s answer satisfying enough. After a few experiments though I think I understood the levels and decided to share: Short answer: Levels are parts of the index or column. Long answer: I think this multi-column DataFrame.groupby example … Read more

Boxplot with pandas groupby multiindex, for specified sublevels from multiindex

this code: data[‘2013-08-17′].boxplot(by=’SPECIES’) Will not work, as boxplot is a function for a DataFrame and not a Series. While in Pandas > 0.18.1 the boxplot function has the argument columns which defines from what column the data is taken from. So data.boxplot(column=’2013-08-17′,by=’SPECIES’) should return the desired result. An example with the Iris dataset: import pandas … Read more

pandas: convert index type in multiindex dataframe

IIUC you need the last level of Multiindex. You could access it with levels: df1.index.levels[-1].astype(str) In [584]: df1.index.levels[-1].astype(str) Out[584]: Index([‘1’, ‘2’, ‘3’, ‘4’, ’96’, ’99’], dtype=”object”, name=”Values”) EDIT You could set your inner level with set_levels method of multiIndex: idx = df1.index df1.index = df1.index.set_levels([idx.levels[:-1], idx.levels[-1].astype(str)])

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