簡體   English   中英

計算數組中每個項目的大寫和小寫字母的數量

[英]Count the number of uppercase and lowercase letters for each item in an array

我正在嘗試編寫一個 function 來計算數組中每個項目的小寫和大寫字母的數量。 然后在我的原始表中創建一個新列,將該實例分類為具有更多大寫或小寫字母。

這是我的數組:

[array([['MARZIA HAS LIGMA LWIAY #0044'],
   ['This Slinky Montage Is Bizarrely Satisfying to Watch'],
   ['MAKING HER DREAM COME TRUE! (MAKE A WISH)'],
   ...,
   ['THE EVOLUTION OF FORTNITE! 2011 - 2019'],
   ["India's trucks are works of art"],
   ['Several airlines change flight routes after Iranian missile downs American drone']],
  dtype=object)]

我有的 function

Upper=0
Lower=0
for c in range(len(dummy_array)):
    for i in c:
        if i.isupper():
            Upper +=1
        elif i.islower():
            Lower +=1
        else:
            pass

        if d[Upper] > d[Lower]:
            train_data["Caps_in_title"] = 1
        else:
            train_data["Caps_in_title"] = 0
print(Upper)
print(Lower)

這應該有效:

import pandas as pd

# setup
df = pd.DataFrame({
    "text" : [['MARZIA HAS LIGMA LWIAY #0044'],
       ['This Slinky Montage Is Bizarrely Satisfying to Watch'],
       ['MAKING HER DREAM COME TRUE! (MAKE A WISH)'],
       ['THE EVOLUTION OF FORTNITE! 2011 - 2019'],
       ["India's trucks are works of art"],
       ['Several airlines change flight routes after Iranian missile downs American drone']]
})

def character_case_ratio(xs):
    """
    function to count number of upper and lowercase letters and compare there ratio
    """
    upper_count = len([x for x in xs[0] if (x.isupper() and x.isalpha())])
    lower_count = len([x for x in xs[0] if (x.islower() and x.isalpha())])

    if upper_count > lower_count:
        return 1
    return 0

# apply the function above over your input text, and create a new column in the DataFrame
df["case_ratio"] = df["text"].apply(character_case_ratio)

Output:

    text                                                case_ratio
0   [MARZIA HAS LIGMA LWIAY #0044]                      1
1   [This Slinky Montage Is Bizarrely Satisfying t...   0
2   [MAKING HER DREAM COME TRUE! (MAKE A WISH)]         1
3   [THE EVOLUTION OF FORTNITE! 2011 - 2019]            1
4   [India's trucks are works of art]                   0
5   [Several airlines change flight routes after I...   0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM