簡體   English   中英

當它們是 nan 時遍歷列

[英]Iterating through columns when they are nan

我正在遍歷列中的單元格,其中一些單元格可能是空的。 打印其中一些值將給出以下 output(數字表示我所在的單元格/索引):

204 ['https://www.somelink.com/random-serial-number-5a4sd5as4d', 'https://www.somelink.com/random-serial-number-asdaws8dqw89d4', 'https://www.somelink .com/random-serial-number-awd89qw89d4qw']

205南

然后我遍歷每個項目並用某些關鍵字替換它們。

import pandas as pd

def main():
    file = pd.read_csv('DATA_TABLE_RAW.csv')
    id_column = file['ID'].str.split('/')
    id_result = id_column.str.get(3)
    id_result = id_result.str.split('-')
    id_result = id_result.str.get(0)
     
    bucketColumn = file['Bucket'].str.split(', ')
    
    bucket = [''] * len(bucketColumn)
    
    for index, column in enumerate(bucketColumn):

        print(index, column)
        if (pd.isna(column)): # problem line
            print(index, " is Empty")
   
        for item in column:
            # print(index)
            if ('5a4sd5as4d' in item):
                bucket[index] += 'Integerity, '

            elif ('asdaws8dqw89d4' in item):
                bucket[index] += 'Resources, '
    
            elif ('awd89qw89d4qw' in item):
                bucket[index] += 'Class, '

# then take that data and save to an excel file.

問題是因為某些字段將是空的並且顯示為nan我無法遍歷它並且我得到錯誤:

對於列中的項目:TypeError:'float' object 不可迭代

所以我的計划是只捕獲空列而不是迭代它們。 在我標記為 #problem # problem line的行上,我嘗試使用isna()但返回以下錯誤:

if (pd.isna(column)): ValueError: 具有多個元素的數組的真值不明確。 使用 a.any() 或 a.all()

還嘗試使用pd.isnull(column)給出了同樣的錯誤:

if (pd.isnull(column)): ValueError: 具有多個元素的數組的真值不明確。 使用 a.any() 或 a.all()

我嘗試做if (not column):但當 205 為空時,這並沒有捕捉到。 或者if (len(column) == 0):並且也沒有捕捉到它。 有沒有辦法在不遇到這些問題的情況下檢測列/數組是否為空?

在您的原始代碼中,出現問題是因為有時當值不是nan時,它是一個值列表,而pd.isna將返回一個列表,因此您不能在那里直接使用if 嘗試這個:

import pandas as pd

def main():
    file = pd.read_csv('DATA_TABLE_RAW.csv')
    id_column = file['ID'].str.split('/')
    id_result = id_column.str.get(3)
    id_result = id_result.str.split('-')
    id_result = id_result.str.get(0)
     
    bucketColumn = file['Bucket'].str.split(', ')
    
    bucket = [''] * len(bucketColumn)
    
    for index, column in enumerate(bucketColumn):

        print(index, column)
        if (column == np.nan): # if condition changed
            print(index, " is Empty")
        else:
            for item in column:
                # print(index)
                if ('5a4sd5as4d' in item):
                    bucket[index] += 'Integerity, '

                elif ('asdaws8dqw89d4' in item):
                    bucket[index] += 'Resources, '
    
                elif ('awd89qw89d4qw' in item):
                    bucket[index] += 'Class, '

# then take that data and save to an excel file.

另一個簡單的解決方案就是這樣做(雖然有點老套):

df.fillna("")

暫無
暫無

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

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