Extracting an element of a list in a pandas column

前端 未结 2 399
离开以前
离开以前 2021-02-08 08:58

I have a DataFrame that contains a list on each column as shown in the example below with only two columns.

    Gamma   Beta
0   [1.4652917656926299, 0.93269352         


        
相关标签:
2条回答
  • 2021-02-08 09:36

    Itertuples would be pretty slow. You could speed this up with the following:

    for column_name in df_params.columns:
        df_params[column_name] = [i[0] for i in df_params[column_name]]
    
    0 讨论(0)
  • 2021-02-08 09:43

    You can use the str accessor for lists, e.g.:

    df_params['Gamma'].str[0]
    

    This should work for all columns:

    df_params.apply(lambda col: col.str[0])
    
    0 讨论(0)
提交回复
热议问题