Python 3.x 熊猫:一个字符串在数据帧单元格中出现多少次?
我相信有一个简单的问题。我有一个熊猫数据帧Python 3.x 熊猫:一个字符串在数据帧单元格中出现多少次?,python-3.x,string,pandas,frequency,Python 3.x,String,Pandas,Frequency,我相信有一个简单的问题。我有一个熊猫数据帧df看起来非常类似: data = [{"Text" : "Dog", "Dog" : 1}, {"Text" : "Cat", "Dog" : 0}, {"Text" : "Mouse", "Dog" : 0}, {"Text" : "Dog", "Dog" : 1}] df = pd.DataFrame(data) 我试图在Text列中搜索大量关键字,并计算它们在每个单元格中出现的次数。结果应该
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__':
我有两个问题:
mykeywords
中的所有关键字,并分别获得一列非常感谢您提前提供的任何帮助 如果可能,文本中有多个值,并且需要计数值:
mykewords = ('Cat', 'Mouse')
for x in mykewords:
df[x] = df.Text.str.count(x)
更好的解决方案是使用单词边界和:
不同的解决方案:
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
data=[{“Text”:“Dog-Cat-Catman”,“Dog”:1},
{“文本”:“猫”,“狗”:0},
{“文本”:“鼠标猫”、“狗”:0},
{“文本”:“狗”,“狗”:1}]
df=pd.DataFrame(数据)
df1=df.copy()
打印(df)
狗文本
01猫狗猫猫猫
10只猫
20只老鼠猫
31只狗
mykewords=(‘猫’、‘老鼠’)
对于mykewords中的x:
df[x]=df.Text.str.findall(r“\b{}\b.format(x)).str.len()
打印(df)
狗文本猫鼠标
0 1狗猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫猫。这对我来说就像是一种魅力
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)
你需要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是的,可以有多个字符串。您好@jezrael,谢谢您的解决方案。尽管如此,我还是从上面得到了错误。知道为什么吗?对不起,我不太明白。对于这两种解决方案,我得到了相同的错误。我如何应用copy()
@Rachel-是否可能在错误前看到您的代码,3行?我明白了!我试图将它应用到我的数据帧的一个切片/副本上,即df.head(5)
。那没用!谢谢大家!@Rachel-你也可以用CopyWarning查看设置的更好解释,9分钟前发布了相同的解决方案,请检查我的答案。对,你的解决方案几乎与我相似,但我在这里使用列表而不是元组@耶斯雷尔。
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)