Question: Consider the following:

```
>>> import numpy as np
>>> import pandas as pd
>>> df = pd.DataFrame(np.random.randn(5, 2), index=[100, 101, 101, 102, 103])
>>> idx = set(df.index)
>>> for id_ in idx:
...     slice = df.loc[id_]
...     # stuff with slice
>>>

```

I need to do stuff with `slice` within the `for` loop but that stuff is predicated on `slice` being a `DataFrame`. `slice` is a `DataFrame` when there are more than one matching records, but a `Series` otherwise. I know `pandas.Series` has the `Series.to_frame` method but `pandas.DataFrame` does not (so I cannot just call `df.loc[id_].to_frame()`).

What is the best way to test and coerce `slice` into a `DataFrame`?

(Is it really as simple as testing if `isinstance(df.loc[id_], pd.Series)`?)

Answer: 
Or use `df[...]` conditioning `df.index`:

```
...
for id_ in idx:
     slice = df[df.index==id_]
     print(slice)

```

Output:

```
            0         1
100  2.751189  1.978744
            0         1
101  0.154483  1.646657
101  1.381725  0.982819
           0         1
102  0.26669  0.032702
            0         1
103  0.186235 -0.481184

```
You can force the variable slice to be a pandas dataframe by using the pd.Dataframe init method as follows:

```
for id_ in idx:
    slice = pd.DataFrame(df.loc[id_])
    print(type(slice))

```

output:

```
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>

```

Then you can treat the variables as Dataframes inside the loop.