我有一个这样的数据框:
print(df.words[0])
[('replacement', 1), ('shaver', 2)]
print(df.words[1])
[('filter', 2), ('purifier', 1), ('please', 2)]
我想创建一个名为“all_words”的新列。该列应该代表真正的字符串,而不是数字。
('head', 3) should be: "head,head,head"
示例的期望输出:
print(df.all_words[0])
'replacement, shaver, shaver'
print(df.all_words[1])
'filter, filter, purifier, please, please'
最佳答案
您将需要应用
一个函数来将元组连接到一个字符串。
df['all_words'] = df.words.apply(lambda x: ', '.join(', '.join([y[0]] * y[1]) for y in x))
关于python - 如何在 DataFrame 中将单词与数字相乘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58216460/