That flatten
function will recurse down if the element is an instance of collections.Iterable
and it's not a string (which is iterable, but we usually want to treat it as a scalar, something we're not going to look inside).
Even though DataFrames
are instances of collections.Iterable
, it sounds like you want them to be terminal too. In that case:
if (isinstance(el, collections.Iterable) and
not isinstance(el, (basestring, pandas.DataFrame))):
After which:
>>> list(flatten([[1,2], "2", df]))
[1, 2, '2', <class 'pandas.core.frame.DataFrame'>
Int64Index: 100 entries, 0 to 99
Data columns (total 4 columns):
A 100 non-null values
B 100 non-null values
C 100 non-null values
D 100 non-null values