python - Persistence problems when using iterrows() -


as believe reported in this thread, filling in dataframe using iterrows() can result in persistence problems. e.g. simple as:

my_dataframe = pd.dataframe(np.nan, index = xrange(5),columns=['foo',  'bar'])  ix, row in my_dataframe.iterrows():   row['foo'] = 'hello' 

results in no changes dataframe:

> my_dataframe     foo  bar 0   nan  nan 1   nan  nan 2   nan  nan 3   nan  nan 4   nan  nan 

and got no warnings, no exceptions, etc. intended? bug? intended? happening?

the above latest stable version of pandas, 0.13.1.

you're changing type of row, , it's modifying copy.

something keeping dtype have worked in case:

in [11]: ix, row in my_dataframe.iterrows():    ....:       row['foo'] = 1 

this behaviour isn't guaranteed, it's better assignment using loc or assigning column directly:

in [12]: row['foo'] = 'hello'  # works  in [13]: row.loc[:, 'foo'] = 'hello'  # works 

see returning view vs copy in docs.

i should add can assigning original frame (using loc/ix), can (and should) avoid vectorising solutions rather iterating on each row:

for ix, row in my_dataframe.iterrows():       my_dataframe.ix[ix, 'foo'] = 'hello'  # works 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -