Skip to content
Advertisement

pandas change dataset value of a specific row using loc

I’m new to programming, I’m working on a python project using pandas I wanted to change values of each row of a dataset using .loc, but it seems like it won’t work, the idea is to make a row take EOL value if the row is equal to 0, the code doesn’t bring an error, but my dataset is unchanged after the iterations. Here is the code:

JavaScript

I think it’s because i’m using .iloc so it won’t change the value of ‘failure_comp1’ in the dataset. But I can’t figure out how to get a specific row from .loc without using .iloc., if anyone as any suggestions I’d be very grateful, thanks Here is the structure of the whole dataset (don’t mind the NaNs): enter image description here Here is what i have for example (for one ‘machine’):

JavaScript

I want it to become this:

JavaScript

It’s a time series dataset and i want to label each component of machines with it’s End Of Life time (number of days), i’ve already got it labeled at the date where it fails, but I want to have it labeled for each row of that specific component.

Advertisement

Answer

So I wouldn’t iterate through the rows (although you could if you want, I’ll show that solution too). But what I would do is use a .groupby('macineID'). 1) Then convert all the 0s to nan. 2) forward fill the nans. 3) this will leave the first 0 as a nan, so finally fillna with 365.

Given as a sample data set:

JavaScript

Code:

JavaScript

If you want to use the .loc or .iloc:

Here’s how I would do it. I would loop through each unique machineID, filter the dataframe to get just those machines, then iterrate through that sub-group. I also would not hard code the i (index). .iteritems() and or iterrows() will returns the index value for you, so just use that.

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement