【问题标题】:Dataframe not updating after running through function运行功能后数据框未更新
【发布时间】:2021-03-10 07:07:52
【问题描述】:

这是我创建的一个函数的小 sn-p。我使用了我在别处定义的另一个函数 Insert_row_,我知道它可以正常工作。

编辑:我继续,只是把整个函数放在这里。

我遇到的问题是,当我运行我的函数时,它返回的数据帧最终与我用作输入的数据帧相同。为了更新数据框,我有什么遗漏吗?我以为每次迭代都会为每个版本分配一个新版本。

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

编辑:

这里是 insert_row_ 函数:

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 

很遗憾,我无法分享任何数据。但“标签”列本质上是 52630,1,2,52630,1,2....等重复 7000 行。

每隔一段时间它会跳过序列中的一个值,我想做的是在它这样做时插入一行 nan,这就是 Insert_row_ 函数成功完成的操作。

希望这能提供更多背景信息!

【问题讨论】:

  • Inser_row_ 函数返回什么?
  • 可能这个条件不起作用? if dataframe['tag'][i] == 52630 请添加您的一些数据,以便我们为您提供帮助。 insert row的一部分也会帮助我们
  • 您好,我添加了插入行功能并提供了一些数据说明,希望对您有所帮助!
  • 我认为过滤掉数据然后对其进行操作然后将其添加回空数据框会更简单。现有方法的可读性很困难。你能抽象出一个可以工作的数据样本吗?

标签: python pandas dataframe


【解决方案1】:

如果您可以保证您的 Insert_row_ 函数正常工作,那么我怀疑您正在使用 for i in dataframe['tag'] 并认为 i 然后索引该列,但实际上, i 是该列中的每个元素。这可能会在接下来的几行中抛出你的比较并跳过整个修改部分,等等。

为了获取索引,您可以使用以下内容:

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

【讨论】:

  • 谢谢!所以我尝试了,它最终改变了数据框,但我最终得到的数据框并不是我想要的。我认为我编写的其余代码可能存在问题。
猜你喜欢
  • 1970-01-01
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-04
  • 1970-01-01
相关资源
最近更新 更多