【问题标题】:Assigning values to an unhashable list in pandas dataframe将值分配给熊猫数据框中的不可散列列表
【发布时间】:2018-06-29 16:49:17
【问题描述】:

我在数据框中有一列包含列表。如下图所示。

我想知道如何在没有任何重复单词的情况下从该列中提取所有单词,并且需要遍历从 0len(uniquewordlist) 的唯一单词列表,并根据迭代为每个单词分配一个值我进去了。

感谢您的帮助。

【问题讨论】:

  • 您可以发布数据样本和预期输出,而不是图片?
  • 您是否要标记每个单词?您可能想查看sklearn 包及其标签。
  • @Vic13 看看我的回答,如果这就是你想要的,请告诉我。

标签: python pandas series


【解决方案1】:

这就是你的数据的样子!

import pandas as pd
df = pd.DataFrame([[['kubernetes', 'client', 'bootstrapping', 'ponda']], [['micro', 'insu']], [['motor', 'upi']],[['secure', 'app', 'installation']],[['health', 'insu', 'express', 'credit', 'customer']],[['secure', 'app', 'installation']],[['aap', 'insta']],[['loan', 'house', 'loan', 'customers']]])

df.columns = ['ingredients']

print(df)

输出:

                                 ingredients
0  [kubernetes, client, bootstrapping, ponda]
1                               [micro, insu]
2                                [motor, upi]
3                 [secure, app, installation]
4   [health, insu, express, credit, customer]
5                 [secure, app, installation]
6                                [aap, insta]
7              [loan, house, loan, customers]

这是显示唯一单词列表的代码。

for i in df.index:

    df.at[i, 'string'] = " ".join(item for item in df.at[i, 'ingredients'])

df.drop(['ingredients'], axis = 1, inplace = True)

from sklearn.feature_extraction.text import CountVectorizer

countvec = CountVectorizer()

counts = countvec.fit_transform(df['string'])

vocab = pd.DataFrame(counts.toarray())
vocab.columns = countvec.get_feature_names()

print(list(vocab.columns))

给予

['aap', 'app', 'bootstrapping', 'client', 'credit', 'customer', 'customers', 'express', 'health', 'house', 'insta', 'installation', 'insu', 'kubernetes', 'loan', 'micro', 'motor', 'ponda', 'secure', 'upi']

您现在有了一个独特词汇列表。如果您可以提供有关如何分配值的更多上下文,我可以继续此答案。

扩展答案:

wordlist = list(vocab.columns)


worddict = {}

for i in range(0, len(wordlist)):

    worddict[wordlist[i]] = i

print(worddict)

【讨论】:

  • 是的,它正在工作,但我的数据非常大,因此 anaconda 没有响应。
  • 现在,我想为所有唯一的单词分配一个唯一的数值。
  • @Vic13 你想如何分配它们,因为我似乎无法理解你为什么以及在什么基础上为它们分配值,如果你告诉我,我会扩展我的代码。
  • 我将使用这些值来预测一些事情。我需要这些词中的一些值,以便我可以用它来训练我的模型。
  • @Vic13 可以吗,如果你有类似{'word1': somevalue, 'word2': anothervalue..}
【解决方案2】:

您可以在字典理解中使用enumerateitertools.chainset 确保映射是唯一的。

来自@Abhishek 的数据。

from itertools import chain

res = {v: k for k, v in enumerate(set(chain.from_iterable(df['ingredients'])))}

print(res)

{'aap': 15,
 'app': 3,
 'bootstrapping': 1,
 ...
 'ponda': 0,
 'secure': 17,
 'upi': 5}

【讨论】:

    【解决方案3】:

    您可以使用不同的一行获得@jpp 的答案(也适用于数据框):

    import pandas as pd
    from collections import Counter
    s = pd.Series([['apple', 'orange', 'raspberry'],
                   ['apple', 'cucumber', 'strawberry', 'orange']])
    s.apply(Counter).sum()
    
    Counter({'apple': 2,
         'cucumber': 1,
         'orange': 2,
         'raspberry': 1,
         'strawberry': 1})
    

    如果你使用

    list(s.apply(Counter).sum().keys())
    

    您得到的正是@Abhishek 的答案,我认为这更具可读性。应用 set 将不起作用,因为没有为集合定义 +

    【讨论】:

      猜你喜欢
      • 2019-06-19
      • 1970-01-01
      • 2020-04-13
      • 2019-09-30
      • 1970-01-01
      • 2022-08-04
      • 1970-01-01
      • 2015-04-14
      • 2019-04-06
      相关资源
      最近更新 更多