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

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

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

  1. For each Concept in a df:
  2. if Note contains ("tupi") -> count word length for these Words.
  3. if not -> count word length for others
  4. print (Concept + " tupi " + word_length)
  5. print (Concept + " not tupi " + word_length)

输出应该是这样的:

  1. ANTEATER tupi 5.034
  2. ANTEATER not tupi 4.56
  3. _______
  4. WILD CAT tupi 4.55
  5. 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 专栏:

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

根据给定数据生成的 Dataframe :

  1. Concept tupi
  2. ANTEATER False 5.0
  3. True 6.5
  4. WILD CAT False 5.5
  5. True 5.5

相关问题