【发布时间】:2016-08-22 16:54:00
【问题描述】:
我有一个大的 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]
如果有人能帮我调试一下,将不胜感激!
【问题讨论】: