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
Post a Comment