What is the method to add a value based on two specific cells in a row of a Dataframe?

Here is the Dataframe layout I am working with: Dataframe layout

This is the code snippet I have written:

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)
    ...

The user provides an input for 'camera1' and the value for 'time_' represents the current date/time.

By submitting the user input, the code checks if 'camera1' matches any entry in the 'Camera' column and if the corresponding 'Return' cell is empty. If this condition is met, the current date/time value ('time_') is added to the row. Otherwise, a new row is created with the new information.

I'm seeking guidance on how to properly insert the 'time_' value into the row where the user input already exists in the dataframe and if the 'Return' column is empty.

Answer №1

I was successfully able to achieve my desired outcome using the code snippet below:

def check_status():
    data_set = df_updated.loc[(df_updated['Camera'] == selected_camera) & (df_updated['Return'].isnull())]
    if data_set.size != 0:
        return data_set.iat[0,4]
    else:
        pass

After that:

if check_status():
    df_updated.loc[(df_updated['Camera'] == selected_camera) & (df_updated['Return'].isnull()), 'Return'] = current_time
    df_updated.to_csv(csv_file_location, index=False)
#....

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips on extracting data from CSV columns for PyTest test cases

Our system has a utility that interacts with APIs and saves the responses to a CSV file called resp.csv. This CSV file contains the API request in column A, headers in column C, payload in column B, response body in column D, and response code in column E ...

Can someone provide me with instructions on how to enable OpenGL Specular Light?

Struggling to get specular lighting to work in my OpenGL project using Python. I've managed to figure out texturing, depth, and basic gameplay, but the specular lighting remains elusive. I've attempted to mimic a flashlight by constantly adjustin ...

Populating a two-dimensional grid to ensure adjacent values are distinct from each other

I am curious to estimate the worst-case scenario of an algorithm that relies on the number of iterations based on how many pixels in an image have neighboring pixels with differing values. Assuming the image is grayscale, I am seeking a method to create an ...

The Django package does not contain a 'model' attribute. The reference to the 'module' is unresolved

While developing a Realtime chat app with Python, Django, and PyCharm, I encountered an error that says "Unresolved reference 'models'". ...

Error encountered while attempting to parse schema in JSON format in PySpark: Unknown token 'ArrayType' found, expected JSON String, Number, Array, Object, or token

Following up on a previous question thread, I want to thank @abiratis for the helpful response. We are now working on implementing the solution in our glue jobs; however, we do not have a static schema defined. To address this, we have added a new column c ...

Struggling to pinpoint the exact element in Python/Selenium

As I work on creating a website manipulation script to automate the process of email mailbox creation on our hosted provider, I find myself navigating new territory in Python and web scripting. If something seems off or subpar in my script, it's beca ...

Ensure that the tkinter submit button handle function is executed only once, unless new and different input is provided

I'm a beginner in tkinter and I'm struggling with a simple method. I want to create a submit button that disables if the same user input is submitted again, but carries out its function when new input is provided. Can anyone provide some assistan ...

What is the best way to include non-ascii characters in Python's regular expressions?

While working with Python's regex, I noticed that using [一三四五六七八九十] will not match any character within the brackets individually. However, if you want to match each character in the brackets separately like 一, you need to specify ...

Python script utilizing Selenium is returning an empty string when trying to extract data from

When I try to retrieve the value from a dynamically loading table by clicking on the TD element, it works fine. However, when I attempt to fetch the text from the same TD element, it returns an empty string despite trying XPATH and CSS Selector methods. H ...

rearrange elements from a copied axis

Below is the code snippet in question: import pandas as pd from pandas import datetime from pandas import DataFrame as df import matplotlib from pandas_datareader import data as web import matplotlib.pyplot as plt import datetime TOKEN = "d0d2a3295349c62 ...

"Having issues with Django not properly applying the JavaScript and CSS files I've linked in

I have completed my project and organized all the necessary files, including index.html, css, js, and settings.py within the appropriate folders. I am encountering an issue with applying a pen from the following source: CodePen index.html <!DOCTYPE h ...

Activate a new browser tab with Selenium

Currently, I am utilizing Selenium with PhantomJS in Python to handle a new window. For the purpose of testing, my approach is as follows: from selenium import webdriver driver = webdriver.PhantomJS() driver.get('http://www.google.com.br') han ...

What steps should I follow to enable Tesseract to recognize the license plate in my Python OpenCV project?

https://i.stack.imgur.com/gurvA.jpghttps://i.stack.imgur.com/L90dj.jpg Everything seems to be working perfectly in my OpenCV code. It successfully identifies the license plate, extracts a black and white version using contours, but unfortunately when I tr ...

When using Scrapy shell, calling the fetch response.css method results in an

I'm diving into web scraping with scrapy, looking for details on a specific medication: Before creating a Python spider, I started by examining the headline using scrapy shell: <h1 class="headline mb-3 fw-bolder">Beipackzettel von AZI ...

I am experiencing issues with the functionality of the Elastic Beanstalk cron job feature

I'm currently working on deploying a website using Elastic Beanstalk. One issue I'm facing is that after crawling and saving a .txt file every hour, the file loads but doesn't seem to function as expected for users accessing it. Only the .t ...

Having trouble importing the OpenCv module into Python

When attempting to run a Python script using OpenVINO, I first type "setupvars" and then execute the script via the command line with "python main.py". However, after doing so, an error message appears: This is what it says: Traceback (most recent call ...

The Django Extended User model fails to assign values to fields upon creation, despite receiving the appropriate arguments

After creating a new user using a registration form I developed, the user is successfully created with an extension that includes a field pointing to the User. However, when passing information like name or birthday during the creation process, it appears ...

Access the JSON data following its conversion from CSV format

I utilized the csv and json libraries to transform a csv file into a json file. The csv file consists of only one column: document "[{'param1': 'value1', 'param2': 'value2', 'param3': value3, 'par ...

What is the method for inserting a clickable link at the top of every page in a Python PDF document?

We are currently in the process of uploading scanned and OCRed documents onto a website and require a way to insert a link on each page. This link will direct users who come across the pages through a search engine to the main index containing related docu ...

What could be causing the version error when running 'pip install json'?

When attempting to install 'pip install json' in my command prompt, I encountered an error - ERROR: Could not find a version that satisfies the requirement json (from versions: none) ERROR: No matching distribution found for json. What steps shou ...