Create a pandas dataframe from a nested lists of unequal lengths Create a pandas dataframe from a nested lists of unequal lengths pandas pandas

Create a pandas dataframe from a nested lists of unequal lengths


The zip_longest function from itertools does this:

>>> import itertools, pandas>>> pandas.DataFrame((_ for _ in itertools.zip_longest(*nest)), columns=['aa', 'bb', 'cc'])    aa    bb    cc0  aa1   bb1   cc11  aa2   bb2   cc22  aa3   bb3   cc33  aa4   bb4  None4  aa5  None  None

If you have an older version of pandas you may need to wrap zip_longest in a list constructor. On older Python you may need to call izip_longest instead of zip_longest.


Option 1

pd.DataFrame(nest, ['aa', 'bb', 'cc']).T    aa    bb    cc0  aa1   bb1   cc11  aa2   bb2   cc22  aa3   bb3   cc33  aa4   bb4  None4  aa5  None  None

Option 2
Homebrew zip_longest

f = lambda x, n: x[n] if n < len(x) else Nonen, m = max(map(len, nest)), len(nest)pd.DataFrame(    [[f(j, i) for j in nest] for i in range(n)],    columns=['aa', 'bb', 'cc'])    aa    bb    cc0  aa1   bb1   cc11  aa2   bb2   cc22  aa3   bb3   cc33  aa4   bb4  None4  aa5  None  None


Or maybe

pd.DataFrame(data={'value':nest},index=['aa', 'bb', 'cc']).value.apply(pd.Series).TOut[1297]:     aa   bb   cc0  aa1  bb1  cc11  aa2  bb2  cc22  aa3  bb3  cc33  aa4  bb4  NaN4  aa5  NaN  NaN