【问题标题】:Pandas: How many times does a string appear in a dataframe cell?Pandas:一个字符串在数据框单元格中出现了多少次?
【发布时间】:2019-03-04 07:04:01
【问题描述】:

我相信有一个简单的问题。我有一个熊猫数据框 df 看起来与此非常相似:

data = [{"Text" : "Dog", "Dog" : 1},
        {"Text" : "Cat", "Dog" : 0}, 
        {"Text" : "Mouse", "Dog" : 0}, 
        {"Text" : "Dog", "Dog" : 1}]

df = pd.DataFrame(data)

我正在尝试在 Text 列中搜索多个关键字,并计算它们在每个单元格中出现的次数。结果应该存储在一个新列中,该列显示找到特定关键字的次数。结果应该就像Dog 列一样。

我尝试使用 pandas str.count。它工作得很好。但是在我尝试将结果存储在新列中的那一刻,我遇到了麻烦:

mykewords = ('Cat', 'Mouse')
df['Cat'] = df.Text.str.count("Cat")

我收到以下错误消息:

A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  if __name__ == '__main__':

我有两个问题:

  1. 我做错了什么,我该如何解决?
  2. 如何循环遍历mykeywords 中的所有关键字并分别获得一列?

非常感谢您提前提供的任何帮助!

【问题讨论】:

  • 你需要df.Text.str.get_dummies()吗?
  • 我也这么认为。你可能需要@coldspeed 的建议。
  • Text 列中只有一个关键字?或者可能像data = [{"Text" : "Dog Cat", "Dog" : 1}, {"Text" : "Cat Cat", "Dog" : 0}, {"Text" : "Mouse Cat", "Dog" : 0}, {"Text" : "Dog", "Dog" : 1}]
  • @jezrael 是的,多个字符串是可能的。

标签: python-3.x string pandas frequency


【解决方案1】:

只需使用最新版本更新 pandas 并尝试以下代码。这对我来说就像一种魅力。

import pandas as pd
data = [{"Text" : "Dog", "Dog" : 1},
        {"Text" : "Cat", "Dog" : 0}, 
        {"Text" : "Mouse", "Dog" : 0}, 
        {"Text" : "Dog", "Dog" : 1}]

df = pd.DataFrame(data)
mykewords = ['Cat', 'Mouse']
for i in mykewords:
    df[i] = df.Text.str.count(i)

【讨论】:

  • 9 分钟前发布了相同的解决方案,请检查我的答案。
  • 对,您的解决方案与我几乎相似,但这里使用的是列表而不是元组。 @jezrael。
【解决方案2】:

如果可能在文本中有多个值并且需要计数值:

mykewords = ('Cat', 'Mouse')
for x in mykewords:
    df[x] = df.Text.str.count(x)

更好的解决方案是使用带有Series.str.findallSeries.str.len 的单词边界:

for x in mykewords:
    df[x] = df.Text.str.findall(r"\b{}\b".format(x)).str.len()

解决方案的区别:

data = [{"Text" : "Dog Cat Catman", "Dog" : 1},
        {"Text" : "Cat Cat", "Dog" : 0}, 
        {"Text" : "Mouse Cat", "Dog" : 0}, 
        {"Text" : "Dog", "Dog" : 1}]

df = pd.DataFrame(data)
df1 = df.copy()
print (df)
   Dog            Text
0    1  Dog Cat Catman
1    0         Cat Cat
2    0       Mouse Cat
3    1             Dog

mykewords = ('Cat', 'Mouse')

for x in mykewords:
    df[x] = df.Text.str.findall(r"\b{}\b".format(x)).str.len()
print (df)
   Dog            Text  Cat  Mouse
0    1  Dog Cat Catman    1      0 <-not match Catman
1    0         Cat Cat    2      0
2    0       Mouse Cat    1      1
3    1             Dog    0      0

for x in mykewords:
    df1[x] = df1.Text.str.count(x)
print (df1)
   Dog            Text  Cat  Mouse
0    1  Dog Cat Catman    2      0 <-match Catman
1    0         Cat Cat    2      0
2    0       Mouse Cat    1      1
3    1             Dog    0      0

【讨论】:

  • 嗨@jezrael,感谢您的解决方案。我仍然从上面得到错误。知道为什么吗?
  • 抱歉,我不太明白。使用这两种解决方案,我都会遇到相同的错误。如何申请copy()
  • @Rachel - 错误前是否可以看到您的代码,3 行?
  • 我明白了!我试图将它应用于我的数据框的切片/副本 - 即df.head(5)。那没有用!谢谢!
  • @Rachel - 你也可以查看this 以更好地解释SettingWithCopyWarning
猜你喜欢
  • 2014-09-02
  • 1970-01-01
  • 2022-11-27
  • 2017-04-28
  • 2013-02-11
  • 1970-01-01
  • 2019-05-01
  • 2021-12-25
  • 2018-12-17
相关资源
最近更新 更多