Pandas selecting by label sometimes return Series, sometimes returns DataFrame

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 [2]: type(df.loc[[3]]) Out[2]: pandas.core.frame.DataFrame In [3]: type(df.loc[[1]]) Out[3]: pandas.core.frame.DataFrame

Is there a simple way to change a column of yes/no to 1/0 in a Pandas dataframe?

method 1 sample.housing.eq(‘yes’).mul(1) method 2 pd.Series(np.where(sample.housing.values == ‘yes’, 1, 0), sample.index) method 3 sample.housing.map(dict(yes=1, no=0)) method 4 pd.Series(map(lambda x: dict(yes=1, no=0)[x], sample.housing.values.tolist()), sample.index) method 5 pd.Series(np.searchsorted([‘no’, ‘yes’], sample.housing.values), sample.index) All yield 0 0 1 0 2 1 3 0 4 0 5 0 6 0 7 0 8 1 9 1 timing given sample timing … Read more