Skip to content Skip to sidebar Skip to footer

Extracting An Element Of A List In A Pandas Column

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.9326935235505321, float] [9

Solution 1:

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])

Solution 2:

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

forcolumn_namein df_params.columns:
    df_params[column_name] = [i[0] foriin df_params[column_name]]

Post a Comment for "Extracting An Element Of A List In A Pandas Column"