Python program to set a specific value to 2 rows in a particular column and leave the 3rd row blank

Viewed 12

Problem example

I am trying to accomplish a task in python. I have data coming in everyday. There is a column called "PIA_Status". PIA_Status for an Application ID can be "Not Started", "Started" and "Completed". Whenever PIA Status = "Completed", the Task Status column should have closed for "Not Started" and "Started". I am trying to achieve this programmatically using python and not sure how to do this. Any help is much appreciated

1 Answers

if i understood what you want, here's somthing that you can work around with.

import pandas as pd

data = pd.read_excel('data.xlsx', sheet_name='Sheet1')
data.loc[data['pia_status'] != 'completed', 'Task Status'] = 'Closed' 
# print(data)
pd.DataFrame(data).to_excel('data.xlsx', sheet_name='Sheet1', index=False)

here's a more readable solution for beginners with a for each and an if statement:

import pandas as pd

data = pd.read_excel('data.xlsx', sheet_name='Sheet1')

for i in range(len(data)):
    if data['pia_status'][i] != 'completed':
        data['Task Status'][i] = 'Closed'
    else:
        data['Task Status'][i] = ''
# print(data)
data.to_excel('data.xlsx', sheet_name='Sheet1', index=False)
Related