数据框中的一列采用以下格式
Row 1 :
Counter({'First': 3, 'record': 2})
Row 2 :
Counter({'Second': 2, 'record': 1}).
我想创建一个具有以下值的新列:
Row 1 :
First First First record record
Row 2 :
Second Second record
最佳答案
我可以通过以下代码自己解决这个问题。它与正则表达式有很大关系。
def transform_word_count(text):
words = re.findall(r'\'(.+?)\'',text)
n = re.findall(r"[0-9]",text)
result = []
for i in range(len(words)):
for j in range(int(n[i])):
result.append(words[i])
return result
df['new'] = df.apply(lambda row: transform_word_count(row['old']), axis=1)
关于python - 如何访问存储为数据帧中的列的集合计数器的元素以在 CountVectorizer 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55672269/