【发布时间】:2019-01-18 20:40:03
【问题描述】:
我正在处理一个文本问题,我的 pandas 数据框包含许多列,其中一个由段落组成。我在输出中需要的是定义的 3 列 -
- 最大单词的长度
- 最大单词的数量(如果有任何相似的长度)
- 此类长度相似的单词的总数。
如果一个单词用空格隔开,我就说明它。使用pythonapply-map寻找答案。
这是一个示例输入数据 -
df = pd.DataFrame({'text':[
"that's not where the biggest opportunity is - it's with heart failure drug - very very huge market....",
"Of course! I just got diagnosed with congestive heart failure and type 2 diabetes. I smoked for 12 years and ate like crap for about the same time. I quit smoking and have been on a diet for a few weeks now. Let me assure you that I'd rather have a coke, gummi bears, and a bag of cheez doodles than a pack of cigs right now. Addiction is addiction.",
"STILLWATER, Okla. (AP) ? Medical examiner spokeswoman SpokesWoman: Oklahoma State player Tyrek Coger died of enlarged heart, manner of death ruled natural."
]})
df
text
0 that's not where the biggest opportunity is - ...
1 Of course! I just got diagnosed with congestiv...
2 STILLWATER, Okla. (AP) ? Medical examiner spok...
这是预期的输出 -
text word_count word_length words
0 that's not where the biggest opportunity is - ... 1 11 opportunity
1 Of course! I just got diagnosed with congestiv... 1 10 congestive
2 STILLWATER, Okla. (AP) ? Medical examiner spok... 2 11 spokeswoman SpokesWoman
【问题讨论】:
-
你认为什么是“单词”?例如,“market....”会算作一个单词吗?
-
是的。用空格隔开的所有东西都是我的
word。 -
我会在你的问题中补充一点,比如“任何由空格(或行首/行尾)分隔的东西都是我认为的单词” - 这是 必不可少的答案。
标签: python-applymap