python—使用for/if循环的字长计数

bqujaahr  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(261)

我有一个 Dataframe ,我需要从列中计算字长 Word 每人 Concept 分别视具体情况而定 Note 专栏。

For each Concept in a df: 
  if Note contains ("tupi") -> count word length for these Words.    
  if not -> count word length for others

  print (Concept + " tupi " + word_length)
  print (Concept + " not tupi " + word_length)

输出应该是这样的:

ANTEATER tupi 5.034

ANTEATER not tupi 4.56
_______
WILD CAT tupi 4.55

WILD CAT not tupi 3.44

输入 Dataframe 示例:
语言概念单词Borrowing note First Antater Tam and Uáyesloan from Tupie第二AntaterãiarúThird Antaterãn fourthat tatãmyeloan from tupififthwild cattamanoys Exthwild catsdfsgyes from Tupiightwild catsdfsgyes loan from tupi

eivgtgni

eivgtgni1#

您可以完全在pandas中执行此操作,而不需要for循环。
创建列 tupi 这表示 Note 列是否包含“tupi”。
创建一个 Word Length 列中单词的长度 Word 专栏。
现在,使用 groupby 并计算每个词的平均字长 Concept 有无“tupi”在 Note 专栏:

df['tupi'] = df['Note'].str.contains('tupi').fillna(False)
df['Word Length'] = df['Word'].str.len()
df.groupby(['Concept', 'tupi'])['Word Length'].mean()

根据给定数据生成的 Dataframe :

Concept   tupi 
ANTEATER  False    5.0
          True     6.5
WILD CAT  False    5.5
          True     5.5

相关问题