【问题标题】:Iterating over Rows and Columns to add counts in Pandas迭代行和列以在 Pandas 中添加计数
【发布时间】:2019-12-14 06:48:46
【问题描述】:

我正在尝试遍历 Pandas 中的列和行以交叉引用我拥有的列表并计算共现。

我的数据框看起来像:

+-------+-----+-----+----+----+-------+-------+------+
| Lemma | Dog | Cat | Sg | Pl |  Good |  Okay |  Bad |
+-------+-----+-----+----+----+-------+-------+------+
| Dog   |   0 |   0 |  0 |  0 |   0   |   0   |  0   |
| Cat   |   0 |   0 |  0 |  0 |   0   |   0   |  0   |
+-------+-----+-----+----+----+-------+-------+------+

我有一个类似的列表:

c=[[dog, Sg, Good], [cat, Pl, Okay], [dog, Pl, Bad]

我想浏览Lemma 中的每一项,在c 中找到它,然后为该列表项查找任何列名。如果看到这些列名,我将添加 +1。如果引理项出现在彼此的 3 个单词窗口中,我还想添加一个计数。

我尝试过类似以下的方法(忽略单词窗口问题):

for idx, row in df.iterrows():
    for columns in df:
        for i in c:
            if i[0]==row:
                if columns in c[1]:
                    df.ix['columns','row'] +=1

但我收到错误消息:“ValueError:系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。 "

我的理想结果如下:

+-------+-----+-----+----+----+-------+-------+------+
| Lemma | Dog | Cat | Sg | Pl |  Good |  Okay |  Bad |
+-------+-----+-----+----+----+-------+-------+------+
| Dog   |   1 |   1 |  1 |  1 |   1   |   0   |  1   |
| Cat   |   2 |   0 |  0 |  1 |   0   |   1   |  0   |
+-------+-----+-----+----+----+-------+-------+------+

谢谢!

【问题讨论】:

  • 我认为您的结果有问题,为什么 Dog 和 lemma Cat 列包含 2 ?
  • 输出看起来很奇怪。为什么Cat-Cat0,而Dog-Cat1

标签: python pandas iteration


【解决方案1】:
  1. 问题中显示的理想结果并不准确。 dog 列中不应有 cat,反之亦然。
  2. 我不会遍历DataFrame,我会将listslist解压成dict,然后将dict加载成DataFrame,如下所示。

代码:

import pandas as pd

c=[['dog', 'Sg', 'Good'], ['cat', 'Pl', 'Okay'], ['dog', 'Pl', 'Bad'],
   ['dog', 'Sg', 'Good'], ['cat', 'Pl', 'Okay'], ['dog', 'Pl', 'Okay'],
   ['dog', 'Sg', 'Good'], ['cat', 'Sg', 'Good'], ['dog', 'Pl', 'Bad'],
   ['dog', 'Sg', 'Good'],['cat', 'Pl', 'Okay'], ['dog', 'Pl', 'Bad']]

Lemma = {'dog': {'dog': 0, 'Sg': 0, 'Pl': 0, 'Good': 0, 'Okay': 0, 'Bad': 0},
         'cat': {'cat': 0, 'Sg': 0, 'Pl': 0, 'Good': 0, 'Okay': 0, 'Bad': 0}}

注意:c 中的 list 中的每个值都是 Lemma 中的 key。参考python dictionaries。例如对于x = ['dog', 'Sg', 'Good']Lemma[x[0]][x[2]]Lemma['dog']['Good'] 相同。 Lemma['dog']['Good']的初始值=0,因此Lemma['dog']['Good']=0+1,那么下次就是1+1,以此类推

for x in c:
    Lemma[x[0]][x[0]] = Lemma[x[0]][x[0]] + 1
    Lemma[x[0]][x[1]] = Lemma[x[0]][x[1]] + 1
    Lemma[x[0]][x[2]] = Lemma[x[0]][x[2]] + 1

df = pd.DataFrame.from_dict(Lemma, orient='index')

输出:

情节

df.plot(kind='bar', figsize=(6, 6))

以编程方式创建dict

listslist 中为dict keys 创建单词sets

outer_keys = set()
inner_keys = set()
for x in c:
    outer_keys.add(x[0])  # first word is outer key
    inner_keys |= set(x[1:])  # all other words

创建dict of dicts:

Lemma = {j: dict.fromkeys(inner_keys | {j}, 0) for j in outer_keys}

最终dict:

{'dog': {'Okay': 0, 'Pl': 0, 'Good': 0, 'Bad': 0, 'Sg': 0, 'dog': 0},
 'cat': {'Okay': 0, 'Pl': 0, 'Good': 0, 'Bad': 0, 'Sg': 0, 'cat': 0}}

【讨论】:

  • 我认为这是推理,所以我会问另一个问题:真实的数据集有 5000 个单词和 500 个变量,我需要这样做。是否建议这样做:for i in range(5000): Lemma[x[0]][x[i]] = Lemma[x[0]][x[i]] + 1
  • 方法是合理的。与任何数据科学一样,第一步和 80% 的时间都花在将数据转换为可用格式上。该字典是手动构建的,如果以编程方式创建dict 会更好。 dicts 不是 python 的唯一数据结构,但它们可能是主要的。
【解决方案2】:

你有几件事需要改变。

1) 您的列表可能需要Dog 而不是dogCat 而不是cat

2) 你可能想要:for column in df.columns 而不是 for columns in df

3) 你可能想要:if i[0] == row['Lemma'] 而不是 if i[0]==row:(这是它的突破点

4) 你可能想要if column in i 而不是if columns in c[1]

5) 你可能想要df.ix[idx, column] += 1 而不是df.ix['columns','row'] +=1

【讨论】:

    猜你喜欢
    • 2018-08-03
    • 2017-01-26
    • 1970-01-01
    • 2021-08-01
    • 2019-08-05
    • 2020-07-09
    • 1970-01-01
    • 2015-08-28
    • 2016-03-11
    相关资源
    最近更新 更多