0

Let

  1. df be some dataframe;
  2. X be a tuple containing the numerical indices of some of df's columns;
  3. df0 be the sub-dataframe of df given by df.iloc[:, X]; and
  4. df1 be some transform of df0 such that len(df1) equals len(df0), and df1.columns equals df0.columns;

Hence df.iloc[:, X] has the same shape as df1.

I want to update the contents of the df.iloc[:, X] sub-dataframe by assigning df1 to it.

If I do the naive thing:

df.iloc[:, X] = df1

most of the updated columns (though not all, curiously enough) get filled with NaNs, even though there are no NaNs in df1.

I figure that this has to do with the indexing of df1 with respect to df, but couldn't find a solution. (One Hail-Mary-Pass thing I tried was setting df1.index = df.index, but it didn't work1.)


1Not surprising, since I don't know what I'm doing, though not for lack of trying. I've read the Pandas book, as well as several tutorials, blogs, etc., and some of the source code too, but working with Pandas remains somewhat Russian-roulette-ish for me.

kjo
  • 33,683
  • 52
  • 148
  • 265
  • Maybe this will help: http://stackoverflow.com/questions/20829748/pandas-assigning-multiple-new-columns-simultaneously and/or http://stackoverflow.com/questions/23307301/pandas-replacing-column-values-in-dataframe – shadowtalker Oct 11 '14 at 15:39
  • `df.iloc[:, X] = df1` should work (unless, perhaps, you do something funny to the index of `df1`). Can you create a minimal example showing the unexpected behaviour and post it here? – Alex Riley Oct 11 '14 at 17:32
  • `df.reset_index(False).iloc[:, X] = df1.reset_index(False)` might be worth a shot... – Woody Pride Oct 12 '14 at 04:32

0 Answers0