I am new to pandas module. And have a quick question on data manipulation:
Suppose I have a table as follows:
Tool | WeekNumber | Status | Percentage
-----|------------|--------|------------
M1 | 1 | good | 85
M1 | 4 | bad | 75
M1 | 7 | good | 90
Based on the condition in Status, I would like to add percentage.
For example:
if the status is "good", then the following rows for subsequent week numbers should be all 100, i.e., next rows should be for weeks 2 and 3 with 100%
if the status is 'bad', the percentage should be 0 for the following week numbers, i.e., 0 for the weeks 5 and 6.
I have some idea on how to approach with the condition, but no idea to add rows:
import os, re
import pandas as pd
df = pd.read_excel("test.xlsx")
add_rows = []
for elem in df.Status:
if elem == "good":
add_rows.append(100)
if elem == "bad":
add_rows.append(0)
df.Percent = pd.Series(add_rows)
However, this only gives me three values based on the condition, and changes the values of specific week numbers. But I want the following:
Tool | WeekNumber | Status | Percentage
-----|------------|--------|------------
M1 | 1 | good | 85
M1 | 2 | good | 100
M1 | 3 | good | 100
M1 | 4 | bad | 75
M1 | 5 | bad | 0
M1 | 6 | bad | 0
M1 | 7 | good | 90
set_index\ andreindex