Force.ix to return DataFrame in pandas

When I use .ix with a DataFrame, is there a way to make pandas always return a DataFrame?

For example, if I run the following lines,

import pandas as pd import numpy as np df = pd.DataFrame(np.arange(6).reshape(3, 2), index=[0, 0, 1]) x = df.ix[0] y = df.ix[1] 

then x will be a DataFrame because 0 appears twice in the index, and y will be a series because 1 is unique in the index. I want y to also have a DataFrame (because I use iterrows (), which is not specific to Series, as a result).

I can check the return type of the .ix file and convert it to a DataFrame if necessary, but I thought there would be a better way.

Note : with pandas v0.20, .ix indexer is deprecated in favor of .iloc / .loc .

+6
source share
1 answer

Yes, index it with a list:

 > x = df.ix[[0]] > y = df.ix[[1]] > type(x) pandas.core.frame.DataFrame > type(y) pandas.core.frame.DataFrame 
+10
source

Source: https://habr.com/ru/post/969359/


All Articles