Dataframe not updating after running through function

Viewed 162

Here's a small snippet of a function I have created. I use another function Insert_row_ that I define elsewhere, which I know works correctly.

EDIT: I went ahead and just put the entire function in here.

The issue that I am having is that when I run my function, the dataframe that it returns just ends up being the same one that I used as input. Is there something I'm missing in order to update the dataframe? I thought I was assigning a new version of each with each iteration.

def check_for_skipped_sensors_and_add_nans(dataframe):
    for i, item in dataframe['tag'].items():
        if item == 52630:
            a = dataframe['tag'][i+1] == 2
            b = dataframe['tag'][i+1] == 52630
            c = dataframe['tag'][i+1] == 1
            if a == True:
                dataframe = Insert_row_(i+2, dataframe, nan_row)
            if b == True:
                dataframe = Insert_row_(i+2, dataframe, nan_row)
                dataframe = Insert_row_(i+3, dataframe, nan_row)
            if c == True:
                pass
        if item == 1:
            a = dataframe['tag'][i+1] == 52630
            b = dataframe['tag'][i+1] == 1
            c = dataframe['tag'][i+1] == 2
            if a == True:
                dataframe = Insert_row_(i+2, dataframe, nan_row)
            if b == True:
                dataframe = Insert_row_(i+2, dataframe, nan_row)
                dataframe = Insert_row_(i+3, dataframe, nan_row)
            if c == True:
                pass
        if item == 2:
            a = dataframe['tag'][i+1] == 1
            b = dataframe['tag'][i+1] == 2
            c = dataframe['tag'][i+1] == 52630
            if a == True:
                dataframe = Insert_row_(i+2, dataframe, nan_row)
            if b == True:
                dataframe = Insert_row_(i+2, dataframe, nan_row)
                dataframe = Insert_row_(i+3, dataframe, nan_row)
            if c == True:
                pass
        if dataframe['tag'][i] == 'nan':
            pass

EDIT:

Here's the insert_row_ function:

def Insert_row_(row_number, df, row_value): 
    # Slice the upper half of the dataframe 
    df1 = df[0:row_number] 
   
    # Store the result of lower half of the dataframe 
    df2 = df[row_number:] 
   
    # Inser the row in the upper half dataframe 
    df1.loc[row_number]=row_value 
   
    # Concat the two dataframes 
    df_result = pd.concat([df1, df2]) 
   
    # Reassign the index labels 
    df_result.index = [*range(df_result.shape[0])] 
   
    # Return the updated dataframe 
    return df_result 

I unfortunately can't share any of the data. But the 'tag' column is essentially 52630,1,2,52630,1,2....etc repeating for 7000 rows.

Every once in a while it skips one of the values in the sequence, what I want to do is insert a row of nans when it does, which is what the Insert_row_ function successfully does.

Hopefully this provides more context!

1 Answers

If you can guarantee that your Insert_row_ function works correctly, then my suspicion is that you are using for i in dataframe['tag'] and thinking that i then indexes that column, but in reality, i is each element in that column. This could then throw off your comparisons in the next couple of lines and skip over the whole modification portion, etc.

In order to get the index, you could use the following:

def check_for_skipped_sensors_and_add_nans(dataframe):
    for i, item in dataframe['tag'].items(): # gets (index, value) tuples
        if item == 52630:
            a = dataframe['tag'][i+1] == 2
            b = dataframe['tag'][i+1] == 52630
            c = dataframe['tag'][i+1] == 1
            if a == True:
                dataframe = Insert_row_(i+2, dataframe, nan_row)
            if b == True:
                dataframe = Insert_row_(i+2, dataframe, nan_row)
                dataframe = Insert_row_(i+3, dataframe, nan_row)
            if c == True:
                pass
    return dataframe
Related