python - 计算 csv 文件的列中字符串的出现次数

标签 python string csv pandas

我有一个很大的 csv 文件(超过 66k 行),我想计算字符串在每行中出现的次数。我特别关注一列,该列中的每一行都有一个小句子,如下所示:

Example of data:
Sam ate an apple and she felt great
Jill thinks the sky is purple but Bob says it's blue
Ralph wants to go apple picking this fall

我知道如何对文本文件执行此操作,但我很难将相同的技术应用于 csv。我一直在使用 pandas 并尝试了几种方法,但它们返回错误代码或空数据帧。

Attempts:
my_file = "NEISS2014.csv"
df = pandas.read_csv(my_file)

df.groupby(df['sentence'].map(lambda x:'apple' if 'apple' in x else x)).sum()
df[df['sentence'].str.contains("apple") == True]

如果有人能帮我调试这个,我将不胜感激!

最佳答案

我认为你可以使用str.count与列句子:

print df
#                                            sentence
#0    Sam ate an apple and she felt great apple apple
#1  Jill thinks the sky is purple but Bob says it'...
#2          Ralph wants to go apple picking this fall

print df.columns
#Index([u'sentence'], dtype='object')

df['count'] = df['sentence'].str.count('apple')
print df
#                                            sentence  count
#0    Sam ate an apple and she felt great apple apple      3
#1  Jill thinks the sky is purple but Bob says it'...      0
#2          Ralph wants to go apple picking this fall      1

关于python - 计算 csv 文件的列中字符串的出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36905967/

相关文章:

java - 十六进制 -> float 转换不准确

powershell - 通过Powershell将sql数据缓慢导出到CSV

java - 如何提高将数据库数据写入 CSV 的性能

python - 应用三参数函数的 'reduce' 列表的 Pythonic 方法是什么?

java - 在比较您知道长度始终为 1 的字符串时,有什么更好的表现?

python - PyQt4 信号和槽

c# - 使用基本字符串操作的字符串模式匹配

sql - CSV 可通过 SQL 查询

python - Django 1.8 : Model doesn't appear in admin panel

python - 查找距离网格位置最近的 n 个数据点