Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Nesting Dataframes

Hello I want to store a dataframe in another dataframe cell. I have a data that looks like this enter image description here

I have daily data which consists of date, steps, and calories. In addition, I have minute by minute HR data of a specific date. Obviously it would be easy to put the minute by minute data in 2 dimensional list but I'm fearing that would be harder to analyze later.
What would be the best practice when I want to have both data in one dataframe? Is it even possible to even nest dataframes?
Any better ideas ? Thanks!

like image 642
Satsuki Avatar asked Jul 24 '18 18:07

Satsuki


People also ask

Can you nest pandas Dataframes?

Yes, it seems possible to nest dataframes but I would recommend instead rethinking how you want to structure your data, which depends on your application or the analyses you want to run on it after.

How do I stack DataFrame in pandas?

When we concatenate DataFrames, we need to specify the axis. axis=0 tells pandas to stack the second DataFrame UNDER the first one. It will automatically detect whether the column names are the same and will stack accordingly. axis=1 will stack the columns in the second DataFrame to the RIGHT of the first DataFrame.

What is Syntaxfor Panda's data frame?

data takes various forms like ndarray, series, map, lists, dict, constants and also another DataFrame. For the row labels, the Index to be used for the resulting frame is Optional Default np. arange(n) if no index is passed. For column labels, the optional default syntax is - np.

How do you stack 2 series pandas?

pandas's library allows two series to be stacked as vertical and horizontal using a built-in command called concat() . This action is usually performed to create a dataframe from two series.


1 Answers

Yes, it seems possible to nest dataframes but I would recommend instead rethinking how you want to structure your data, which depends on your application or the analyses you want to run on it after.

How to "nest" dataframes into another dataframe

Your dataframe containing your nested "sub-dataframes" won't be displayed very nicely. However, just to show that it is possible to nest your dataframes, take a look at this mini-example:

Here we have 3 random dataframes:

>>> df1
          0         1         2
0  0.614679  0.401098  0.379667
1  0.459064  0.328259  0.592180
2  0.916509  0.717322  0.319057
>>> df2
          0         1         2
0  0.090917  0.457668  0.598548
1  0.748639  0.729935  0.680409
2  0.301244  0.024004  0.361283
>>> df3
          0         1         2
0  0.200375  0.059798  0.665323
1  0.086708  0.320635  0.594862
2  0.299289  0.014134  0.085295

We can make a main dataframe that includes these dataframes as values in individual "cells":

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

We can then access these nested datframes as we would access any value in any other dataframe:

>>> df['dfs'].iloc[0]
          0         1         2
0  0.614679  0.401098  0.379667
1  0.459064  0.328259  0.592180
2  0.916509  0.717322  0.319057
like image 194
sacuL Avatar answered Sep 30 '22 18:09

sacuL