Pandas: Storing Dataframe in Dataframe

前端 未结 3 948
梦如初夏
梦如初夏 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条回答
  • 2021-01-22 15:30

    The 'pointers' referred to at the end of ns63sr's answer could be implemented as a class, e.g...

    Definition:

    class df_holder:
        def __init__(self, df): 
            self.df = df
    

    Set:

    df.loc[0,'df_holder'] = df_holder(df)
    

    Get:

    df.loc[0].df_holder.df
    
    0 讨论(0)
  • 2021-01-22 15:38

    Instead of big_DataFrame['Signal 1'].loc['Name 1'] you should use

    big_DataFrame.loc['Name 1','Signal 1']
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题