Vinod
Vinod

Reputation: 350

drop a single tuple from a multi tuple column

I have the following dataframe:

<bound method DataFrame.info of <class 'pandas.core.frame.DataFrame'>
MultiIndex: 369416 entries, (datetime.datetime(2008, 1, 2, 16, 0), 'ABC') to     (datetime.datetime(2010, 12, 31, 16, 0), 'XYZ')
Data columns:
b_val    369416  non-null values
dtypes: float64(1)>

From this, I want a dataframe that has dates as the indexes and 'ABC' to 'XYZ' as column names with the values as the values under the column 'b_val'. I tried to do:

new_data = new_data.unstack()

But this gives me:

<bound method DataFrame.info of <class 'pandas.core.frame.DataFrame'>
Index: 757 entries, 2008-01-02 16:00:00 to 2010-12-31 16:00:00
Columns: 488 entries, ('b_val', 'ABC') to ('b_val', 'XYZ')
dtypes: float64(488)>

Is there a way to transform this another way or is there a way to drop 'b_val' from each of the column names?

Upvotes: 3

Views: 1777

Answers (1)

Andy Hayden
Andy Hayden

Reputation: 375745

I think unstack is the correct way to do what you've done.

You could drop the first level from the the column names (a MultiIndex) using droplevel:

df.columns = df.columns.droplevel(0)

Here's an example:

df = pd.DataFrame([[1, 'a', 22], [1, 'b', 27], [2, 'a', 35], [2, 'b', 56]], columns=['date', 'name', 'value']).set_index(['date','name'])
df1 = df.unstack()

In [3]: df1
Out[3]:
      value
name      a   b
date
1        22  27
2        35  56

In [4]: df1.columns = df1.columns.droplevel(0)

In [5]: df1
Out[5]:
name   a   b
date
1     22  27
2     35  56

However, a cleaner option is just to unstack the column (the series):

In [6]: df.value.unstack()
Out[6]:
name   a   b
date
1     22  27
2     35  56

Upvotes: 2

Related Questions