Pandas: Storing Dataframe in Dataframe

前端 未结 3 952
梦如初夏
梦如初夏 2021-01-22 15:19

I am rather new to Pandas and am currently running into a problem when trying to insert a Dataframe inside a Dataframe.

What I want to do: I have multiple simulations a

3条回答
  •  猫巷女王i
    2021-01-22 15:51

    the docs say that only Series can be within a DataFrame. However, passing DataFrames seems to work as well. Here is an exaple assuming that none of the columns is in MultiIndex:

    import pandas as pd
    
    signal_df = pd.DataFrame({'X': [1,2,3],
                              'Y': [10,20,30]}  )
    
    big_df = pd.DataFrame({'SimName': ['Name 1','Name 2'],
                           'Date ':[123  , 456 ],
                           'Parameter 1':['XYZ', 'XYZ'],
                           'Parameter 2':['XYZ', 'XYZ'],
                           'Signal 1':[signal_df, signal_df],
                           'Signal 2':[signal_df, signal_df]}  )
    
    big_df.loc[0,'Signal 1']
    big_df.loc[0,'Signal 1'][X]
    

    This results in:

    out1:    X  Y
          0  1  10
          1  2  20
          2  3  30
    
    out2: 0    1
          1    2
          2    3
          Name: X, dtype: int64
    

    In case nested dataframes are not properly working, you may implement some sort of pointers that you store in big_df that allow you to access the signal dataframes stored elsewhere.

提交回复
热议问题