Home > OS >  `df.loc` return series instead dataframe
`df.loc` return series instead dataframe

Time:08-14

I have a DataFrame

import pandas as pd
import numpy as np

index = pd.MultiIndex.from_product([["A", "B"], ["AA", "BB"]])
columns = pd.MultiIndex.from_product([["X", "Y"], ["XX", "YY"]])

df = pd.DataFrame([[1,2,3,4],
                   [5,6,7,8],
                   [9,10,11,12],
                   [13,14,15,16]], index = index, columns = columns)

and a slice slice_ = ((slice(None), slice(None)), ("X", "XX")). Unfortunately df.loc[slice_] returns Series instead DataFrame. How can I fix that?

CodePudding user response:

Two options.

Either wrap the columns' slice in []:

slice_ = ((slice(None), slice(None)), [("X", "XX")])
df.loc[slice_] 

or, if you don't want to alter the slice, convert back to_frame

slice_ = ((slice(None), slice(None)), ("X", "XX"))
df.loc[slice_].to_frame()

output:

       X
      XX
A AA   1
  BB   5
B AA   9
  BB  13

CodePudding user response:

Try with:

df.loc[((slice(None), slice(None)), [("X", "XX")])]
  • Related