0

Following Problem I got a DataFrame like so:

df = pd.DataFrame(columns=pd.MultiIndex(levels=[["foo","baa"],["x","y","z"]], labels=[[0,0,0,1,1,1],[0,1,2,0,1,2]]), index=[0,1,2,3], data=np.random.rand(4,6))

         foo                           baa                    
          x         y         z         x         y         z
0  0.263780  0.942880  0.176744  0.914854  0.920735  0.573692
1  0.601317  0.584691  0.288536  0.832064  0.095142  0.186045
2  0.807323  0.075620  0.399703  0.936894  0.168441  0.468984
3  0.871293  0.324817  0.395784  0.133541  0.365586  0.615219

And I want to get say foo out of it into a new DataFrame completly empty constructed like this:

r_df = pd.DataFrame(index=df.index)
Empty DataFrame
Columns: []
Index: [0, 1, 2, 3]

it should look like this:

         foo
          x         y         z
0  0.263780  0.942880  0.176744
1  0.601317  0.584691  0.288536
2  0.807323  0.075620  0.399703
3  0.871293  0.324817  0.395784

How I tryed to do it was like this:

for label in df.columns.levels[0]:
    data = df[label]
    for row in data.index:
        r_df.loc[row,(label,"x")] = data["x"]
        r_df.loc[row,(label,"y")] = data["y"]
        r_df.loc[row,(label,"z")] = data["z"]

I thought this could work because multi index can be referenced by tubles but it didnt.

7
  • 1
    Simply use print(df['foo']).....You will get foo part. Commented Oct 10, 2018 at 9:48
  • that is not the problam I need to not print but work on the data Commented Oct 10, 2018 at 9:50
  • 1
    You can create new dataframe out of the existing one :- dfnew=df['foo'] Commented Oct 10, 2018 at 9:52
  • 1
    You can also use r_df = df.xs('foo', axis=1, level=0, drop_level=False) if you do want to keep it as a multiindex. Commented Oct 10, 2018 at 9:59
  • If you are satisfied by answer please mark it accepted...let me know if you need any other information. Commented Oct 10, 2018 at 9:59

1 Answer 1

1

Try the following using df.xs to select from multiIndex and keep level:

r_df = df.xs('foo', axis=1, level=0, drop_level=False)

If you want to append a new column under foo then you can do:

df.loc[:,('foo','m')] = 0

if you want to a append a new column in level=0 (same level as foo)

# with no col in level 1
df.loc[:, 'bar'] = 0 

# with col in level 0 and 1
df.loc[:,('bar','m')] = 0
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.