Why there is an extra index when using apply in Pandas
问题 When I use apply to a user defined function in Pandas, it looks like python is creating an additional array. How could I get rid of it? Here is my code: def fnc(group): x = group.C.values out = x[np.where(x < 0)] return pd.DataFrame(out) data = pd.DataFrame({'A':np.random.randint(1, 3, 10), 'B':3, 'C':np.random.normal(0, 1, 10)}) data.groupby(by=['A', 'B']).apply(fnc).reset_index() There is this weird Level_2 index created. Is there a way to avoid creating it when running my function? A B