简体   繁体   中英

Getting a maximum value from a list in pandas columns

I have the following dataframe.

df

Col1                     Col2           Col3
0.00               [50.00, 100.00]      Tall
50.00                     0.00           NaN
[0.00, 50.00, 60.00]      10.00         Short  

I would like to apply max-of-all in the list values and would like to get the following result.

Col1        Col2       Col3
0.00       100.00      Tall
50.00       0.00       NaN
60.00      10.00      Short

I have tried this but couldn't succeed.

df = df.apply(lambda x: max(map(int, x.split(','))))

Can any one help on this?

Method1:

You can use applymap here which will check if the instance is a list, return max of list else return element as is:

out = df.applymap(lambda x: max(x) if isinstance(x,list) else x)

Method 2:

You can stack the dataframe and then apply the function on series and then unstack to get original shape:

out = df.stack().apply(lambda x: max(x) if isinstance(x,list) else x).unstack()

print(out)

   Col1   Col2   Col3
0   0.0  100.0   Tall
1  50.0    0.0    NaN
2  60.0   10.0  Short

Note that this assumes that the rows with list are actual python lists and not a string representation of a list.

You can also use this:

df = df[df.columns].apply(lambda x: x.explode().groupby(level=0).max())

OUTPUT

   Col1  Col2   Col3
0   0.0   100   Tall
1  50.0     0    NaN
2  60.0    10  Short

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM