Pandas selecting by label sometimes return series, sometimes returns dataframe
In Pandas, when I select a label that only has one entry in the index I get back a Series, but when I select an entry that has more then one entry I get back a data frame.
Why is that? Is there a way to ensure I always get back a data frame?
In : import pandas as pd In : df = pd.DataFrame(data=range(5), index=[1, 2, 3, 3, 3]) In : type(df.loc) Out: pandas.core.frame.DataFrame In : type(df.loc) Out: pandas.core.series.Series
Granted that the behavior is inconsistent, but I think it's easy to imagine cases where this is convenient. Anyway, to get a DataFrame every time, just pass a list to
loc. There are other ways, but in my opinion this is the cleanest.
In : type(df.loc[]) Out: pandas.core.frame.DataFrame In : type(df.loc[]) Out: pandas.core.frame.DataFrame