Create and set an element of a Pandas DataFrame to a list

后端 未结 3 1597
野的像风
野的像风 2021-01-13 01:14

I have a Pandas DataFrame that I\'m creating row-by-row (I know, I know, it\'s not Pandorable/Pythonic..). I\'m creating elements using .loc like so



        
相关标签:
3条回答
  • 2021-01-13 02:12

    You need to make sure two things:

    1. there is precisely one entry for that loc,
    2. the column has dtype object (actually, on testing this seems not to be an issue).

    A hacky way to do this is to use a Series with []:

    In [11]: df = pd.DataFrame([[1, 2], [3, 4]], columns=['A', 'B'])
    
    In [12]: df.loc[[0], 'A'] = pd.Series([[]])
    
    In [13]: df
    Out[13]:
        A  B
    0  []  2
    1   3  4
    

    pandas doesn't really want you use [] as elements because it's usually not so efficient and makes aggregations more complicated (and un-cythonisable).


    In general you don't want to build up DataFrames cell-by-cell, there is (almost?) always a better way.

    0 讨论(0)
  • 2021-01-13 02:13

    You can use pd.at instead:

    df = pd.DataFrame()
    df['B'] = [1, 2, 3]
    df['A'] = None
    df.at[1, 'A'] = np.array([1, 2, 3])
    

    When you use pd.loc, pandas thinks you are interacting with a set of rows. So if you try to assign an array using pd.loc, pandas will try to match each element of an array with a corresponding element accessed by pd.loc, hence the error.

    0 讨论(0)
  • 2021-01-13 02:17

    The answer by MishaTeplitskiy works when the index label is 0. More generally, if you want to assign an array x to an element of a DataFrame df with row r and column c, you can use:

    df.loc[[r], c] = pd.Series([x], index = [r])
    
    0 讨论(0)
提交回复
热议问题