Trying to join 2 Dataframes, and store certain data as an array in one cell

前端 未结 2 1352
无人共我
无人共我 2021-01-14 20:57

I have the following 3 data frames:

Frist dataframe:

DF1: 

iID data1 data2

10  blue  green

11  red   teal

Second dataframe:

相关标签:
2条回答
  • 2021-01-14 21:31

    IIUC

    df1.merge(df2.groupby('iID').agg(lambda x : x.tolist()).reset_index())
    Out[144]: 
       iID data1  data2        rH      repH
    0   10  blue  green  [50, 60]  [60, 70]
    1   11   red   teal      [70]      [50]
    
    0 讨论(0)
  • 2021-01-14 21:46

    Worth to add below add ons..

    join, which is left join by default:

    df1.join(df2)
    

    Or concat, which is outer join by default:

    pd.concat([df1, df2], axis=1)
    

    Just adding more narrative:

    >>> df1 = pd.DataFrame({'a':range(6),
    ...                     'b':[5,3,6,9,2,4]}, index=list('abcdef'))
    >>> df2 = pd.DataFrame({'c':range(4),
    ...                     'd':[10,20,30, 40]}, index=list('abhi'))
    >>>
    >>>
    >>> df1
       a  b
    a  0  5
    b  1  3
    c  2  6
    d  3  9
    e  4  2
    f  5  4
    >>> df2
       c   d
    a  0  10
    b  1  20
    h  2  30
    i  3  40
    >>> df4 = df1.join(df2)
    >>> df4
       a  b    c     d
    a  0  5  0.0  10.0
    b  1  3  1.0  20.0
    c  2  6  NaN   NaN
    d  3  9  NaN   NaN
    e  4  2  NaN   NaN
    f  5  4  NaN   NaN
    
    0 讨论(0)
提交回复
热议问题