How to store a numpy arrays in a column of a Pandas dataframe?

后端 未结 2 1876
说谎
说谎 2021-01-07 22:16

Is it possible to store arbitrary numpy arrays as the values of a single column in a dataframe of Pandas?

The arrays are all 2-dim

相关标签:
2条回答
  • 2021-01-07 23:07

    What do you mean store arbitrary numpy arrays as the values of a column in a dataframe of Pandas?

    Something like this?

    import numpy as np
    import pandas as pd
    
    
    x = np.random.randn(50, 25)
    random_frame = pd.DataFrame(x)
    

    This will store the array x in a DataFrame where the column names are 0, 1, 2, 3... Could you clarify? I think this is more a comment, but I don't know if I can comment yet.

    0 讨论(0)
  • 2021-01-07 23:11

    Store them as elements as you would do for any other data:

    import numpy as np
    import pandas as pd
    a = np.arange(10).reshape(2,5)
    b = np.arange(10, 20).reshape(2,5)
    pd.DataFrame({'foo':[42,51], 'arr':[a,b]})
    Out[10]: 
                                                arr  foo
    0            [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]   42
    1  [[10, 11, 12, 13, 14], [15, 16, 17, 18, 19]]   51
    

    Note that what you try to do sounds more to use a Panel.

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