When using a for loop with a dictionary of dataframes, DataFrame.drop() raises an unexpected error, apparently unable to find the label, even when that label exists and is handled correctly in a single df.
A small, complete example of the issue
>>> for key in dfs2:
>>> dfs2[key].drop('0',axis=0,inplace=True)
---------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-179-3b991fac4b08> in <module>()
1 for key in dfs2:
----> 2 dfs2[key].drop('0',axis=0,inplace=True)
/Library/Python/2.7/site-packages/pandas/core/generic.pyc in drop(self, labels, axis, level, inplace, errors)
1875 new_axis = axis.drop(labels, level=level, errors=errors)
1876 else:
-> 1877 new_axis = axis.drop(labels, errors=errors)
1878 dropped = self.reindex(**{axis_name: new_axis})
1879 try:
/Library/Python/2.7/site-packages/pandas/indexes/base.pyc in drop(self, labels, errors)
3049 if errors != 'ignore':
3050 raise ValueError('labels %s not contained in axis' %
-> 3051 labels[mask])
3052 indexer = indexer[~mask]
3053 return self.delete(indexer)
ValueError: labels ['0'] not contained in axis
Iterating through a dict with DataFrame.drop does behave as expected (viz. successfully drop the row) when using errors='ignore'
. However, I expected iteration with drop to behave as drop does with a single dataframe from the same dict:
Expected Output
>>> dfs2[key]
A B C D
a 1 2 3 4
b 2 3 4 5
0 3 4 5 6
d 4 5 6 7
e 5 6 7 8
>>> dfs2[key].drop('0',axis=0,inplace=True)
>>> dfs2[key]
A B C D
a 1 2 3 4
b 2 3 4 5
d 4 5 6 7
e 5 6 7 8
If different behavior for .drop when iterating is intended, a note in the docs might be helpful.
Output of pd.show_versions()
Comment From: TomAugspurger
Can you edit your post to include a copy-patsable example (so how to generate dfs2
)?
Comment From: jorisvandenbossche
@katelaurel I am going to close this issue, but happy to reopen when you can provide a reproducible example.