简体   繁体   English

如何将 append 从 dataframe 到列表?

[英]How to append from a dataframe to a list?

I have a dataframe called pop_emoj that has two columns (one for the emoji, and one for the emoji count) as seen below.我有一个名为 pop_emoj 的 dataframe 有两列(一列用于表情符号,一列用于表情符号计数),如下所示。

☕    585
🌭    193
🌮    186
🌯     85
🌰     53
🌶    124
🌽    138
🍄     46
🍅    170
🍆    506

I have sorted the df based on the counts in descending order as seen below.我已经根据降序对 df 进行了排序,如下所示。

emoji_updated = pop_emoj.head(105).sort_values(ascending=False)
🍻    1809
🎂    1481
🍔    1382
🍾    1078
🥂    1028

And I'm trying to use the top n emojis to append to a new list called top_list, but I am getting stuck.我正在尝试将前n 个表情符号用于 append 到一个名为 top_list 的新列表,但我卡住了。 Here is my code so far.到目前为止,这是我的代码。

def top_number_of_emojis(n):
    
    top_list = []
    top_list = emoji_updated[0].tolist()
    
    return top_list

I'm wanting to take all of column 1 (the emojis) and append them to my list (top_number_of_emojis).我想将所有第 1 列(表情符号)和 append 加入我的列表(top_number_of_emojis)。 The output should look like this: output 应如下所示:

top_number_of_emojis(1) == ['🍻']
top_number_of_emojis(2) == ['🍻', '🎂']
top_number_of_emojis(3) == ['🍻', '🎂', '🍔']

if you already got the top 5 emojis, you just need to save them to a list.如果您已经获得了前 5 个表情符号,您只需将它们保存到一个列表中。 An option for that is iterrows.一个选项是 iterrows。

top_list = []

for id, emoji in emoji_updated.iterrows():
    top_list.append(emoji)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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