For programming purpose, I want .iloc
to consistently return a data frame, even when the resulting data frame has only one row. How to accomplish this?
Currently, .iloc
returns a Series when the result only has one row. Example:
In [1]: df = pd.DataFrame({'a':[1,2], 'b':[3,4]})
In [2]: df
Out[2]:
a b
0 1 3
1 2 4
In [3]: type(df.iloc[0, :])
Out[3]: pandas.core.series.Series
This behavior is poor for 2 reasons:
.iloc
can either return a Series or a Data Frame, forcing me to manually check for this in my code- (wrong info, as pointed out in the comment).loc
, on the other hand, always return a Data Frame, making pandas
inconsistent within itself
For the R user, this can be accomplished with drop = FALSE
, or by using tidyverse's tibble, which always return a data frame by default.