python - 如何访问存储为数据帧中的列的集合计数器的元素以在 CountVectorizer 中使用

标签 python pandas collections

数据框中的一列采用以下格式

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/

相关文章:

python - 如何使用 QAbstractTableModel 控制 QTableView 的 Header

python - 为什么下划线不能匹配 '\W' ?

python - 只清除 turtle 图形中的一部分屏幕

python - 在没有数据透视表的情况下展平 pandas DataFrame

java - 如何将 arraylist 的多个项目与特定位置的另一个 arraylist 进行比较?

java - 打印包含 StringBuffer 对象的 ArrayList

c# - 如何在 .NET 中创建值类型集合?

python - 如何组织Python源代码文件?

Python:如何摆脱嵌套循环?

python - pandas-python 数据框更新列