简体   繁体   中英

Get the largest element of a list from a DataFrame column

I have a DataFrame like this:

df = pd.DataFrame({
    'Names':[['John','Stefan'], ['Stacy','Jennifer'], ['Paul','Sean', 'Alu']],
})

What I would like to do is to create a new column with the longest word present in a list from column "Names". Also, in case there are 2 or more words with the same largest number of char in them, I would like to return both.

So the output should look like this:

| Names             | Output      |
| ----------------- | ------------|
| [John, Stefan]    | Stefan      |
| [Stacy, Jennifer] | Jennifer    |
| [Paul, Sean, Alu] | Paul, Sean  |

I know that for a single list one can do maybe something like this:

sorted = sorted(my_list, key=len)
largest_element = sorted[-1]

But how to iterate in case of a list in a DataFrame column and how to extract more than 1 largest element in case there is a tie in the number of max char?

Does anybody know?

Try:

def get_max(x):
    m = len(max(x, key=len))
    return ', '.join(w for w in x if len(w) == m)


df['Output'] = df['Names'].apply(get_max)
print(df)

Prints:

               Names      Output
0     [John, Stefan]      Stefan
1  [Stacy, Jennifer]    Jennifer
2  [Paul, Sean, Alu]  Paul, Sean

You can write a function and apply it to every row.

def get_largest(names_list):
    sorted_list = sorted(names_list, key=len)
    largest_word = sorted_list[-1]
    longest_length = len(largest_word)
    largest_words = [word for word in names_list if len(word)==longest_length]
    return largest_words

df = pd.DataFrame({'Names': [['John', 'Stefan'], ['Stacy', 'Jennifer'], ['Paul', 'Sean', 'Alu']]})
df['Output'] = df['Names'].apply(get_largest)

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