Pandas Merge row data with multiple values to Python list for a column

前端 未结 2 433
没有蜡笔的小新
没有蜡笔的小新 2020-12-31 18:55

I have a data-frame that looks like

DATA

*id*,             *name*,                      *URL*,                 *Type*  
    2,             birth_f         


        
相关标签:
2条回答
  • 2020-12-31 19:25

    I think you need groupby and aggregate tuple and then convert to list:

    df = df.groupby(['id','name']).agg(lambda x: tuple(x)).applymap(list).reset_index()
    
    print (df)
       id                    name  \
    0   2  birth_france_by_region   
    1   3                long_lat   
    2   4      random_time_series   
    3   5             birth_names   
    
                                                     URL          Type  
    0                 [http://abc.cm, http://pt.python]      [T1, T2]  
    1                  [http://abc.cm, http://pqur.com]      [T3, T1]  
    2            [http://sadsdc.com, http://sadcadf.com]      [T2, T3]  
    3  [http://google.;com, http://helloworld.com, ht...  [T1, T2, T3] 
    

    Because in version 0.20.3 raise error:

    df = df.groupby(['id','name']).agg(lambda x: x.tolist())
    

    ValueError: Function does not reduce

    0 讨论(0)
  • 2020-12-31 19:27

    This will give you the expected result for the "URL" column:

    test.groupby(["id", "name"])['URL'].apply(list)
    
    id  name                  
    2   birth_france_by_region                 [http://abc. com, http://pt. python]
    3   long_lat                                [http://abc. com, http://pqur. com]
    4   random_time_series                [http://sadsdc. com, http://sadcadf. com]
    5   birth_names               [http://google. com, http://helloworld. com, h...
    

    However, I can't find a solution for both URL and Type columns.

    I could propose to do it in 2 steps:

    • temp_table1 = test.groupby(["id", "name"])['URL'].apply(list)
    • temp_table2 = test.groupby(["id", "name"])['Type'].apply(list)
    • Merge temp_table1 & temp_table2
    0 讨论(0)
提交回复
热议问题