【问题标题】:How to insert a value based on two cells within a row (Dataframe)?如何根据一行中的两个单元格插入一个值(数据框)?
【发布时间】:2021-12-06 11:20:58
【问题描述】:

我有以下数据框: Dataframe layout

我写了以下内容:

if (df_.loc[(df_['Camera'] == camera1) & (df_['Return'].isnull())]):
    df_.loc[(df_['Camera'] == camera1) & (df_['Return'].isnull()), 'Return'] = time_
    df_.to_csv(csv_file, index=False)
else:
    df_ = df_.append(dfin, ignore_index = True)
    df_.to_csv(csv_file, index=False)
    ...

camera1 是用户输入的,time_ 是今天的日期/时间。

当用户输入提交时,第一个条件将检查输入是否在第一列“相机”中,第二个条件检查名称为“返回”的列是否为空,如果为真则添加当前日期/时间 (time_) ,否则使用新信息创建新行。

只是寻找如何将 time_ 值添加到行中,如果用户输入已经在数据框中,并且“返回”列是空的。

【问题讨论】:

    标签: python dataframe insert row cell


    【解决方案1】:

    能够通过以下方式得到我想要的:

    def check():
        set = df_.loc[(df_['Camera'] == camera1) & (df_['Return'].isnull())]
        if set.size != 0:
            return set.iat[0,4]
        else:
            pass
    

    然后:

    if check():
        df_.loc[(df_['Camera'] == camera1) & (df_['Return'].isnull()), 'Return'] = time_
        df_.to_csv(csv_file, index=False)
    #....
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-02
      • 2015-02-01
      • 1970-01-01
      相关资源
      最近更新 更多