How to Create a Nested DataFrame in Pandas (With Example)


You can use the following syntax to nest multiple pandas DataFrames within another DataFrame:

df_all = pd.DataFrame({'idx':[1,2,3], 'dfs':[df1, df2, df3]})

This particular example nests three DataFrames (df1, df2, df3) inside a larger DataFrame called df_all.

You can then use the following syntax to access one of the specific nested DataFrames:

#display first nested DataFrame
print(df_all['dfs'].iloc[0])

The following example shows how to use this syntax in practice.

Example: Create Nested DataFrame in Pandas

Suppose we have three pandas DataFrames:

import pandas as pd

#create first DataFrame
df1 = pd.DataFrame({'item': ['A', 'B', 'C', 'D', 'E'],
                    'sales': [18, 22, 19, 14, 30]})

print(df1)

  item  sales
0    A     18
1    B     22
2    C     19
3    D     14
4    E     30

#create second  DataFrame
df2 = pd.DataFrame({'item': ['F', 'G', 'H', 'I', 'J'],
                    'sales': [10, 12, 13, 13, 19]})

print(df2)

  item  sales
0    F     10
1    G     12
2    H     13
3    I     13
4    J     19

#create third DataFrame
df3 = pd.DataFrame({'item': ['K', 'L', 'M', 'N', 'O'],
                    'sales': [41, 22, 28, 25, 18]})

print(df3)

  item  sales
0    K     41
1    L     22
2    M     28
3    N     25
4    O     18

Now suppose that we would like to create one big DataFrame to hold all three of these DataFrames.

We can use the following syntax to do so:

df_all = pd.DataFrame({'idx':[1,2,3], 'dfs':[df1, df2, df3]})

We can then use the pandas iloc function to access specific nested DataFrames.

For example, we can use the following syntax to access the first nested DataFrame:

#display first nested DataFrame
print(df_all['dfs'].iloc[0])

  item  sales
0    A     18
1    B     22
2    C     19
3    D     14
4    E     30

Or we could use the following syntax to access the second nested DataFrame:

#display second nested DataFrame
print(df_all['dfs'].iloc[1])

  item  sales
0    F     10
1    G     12
2    H     13
3    I     13
4    J     19

And so on.

Additional Resources

The following tutorials explain how to perform other common functions in pandas:

How to Convert Index to Column in Pandas
How to Rename Index in Pandas
How to Set Column as Index in Pandas

Leave a Reply

Your email address will not be published. Required fields are marked *