[enter image description here]

IMAGE 2 (results) enter image description here

2I'm working with data manipulation in csvs using python + pandas. Example #s

PL T
0.569999695

0.960002899

1.729999542

1.590000153

1.450000763

1.530002594 1.530002594

1.770000458

0.890003204

0.490001678

1.11000061

1.340000153

2.390003204 2.390003204

2.470001221

2.760002136

3.200000763

2.880001068

2.479999542 2.479999542

3.100002289

1.269996643

1.269996643

1.590000153

2.13999939

I have used if statements in the past however this one is very specific and I can't seem to get the syntax working correctly.

If you look at the code I want column 'T' to be calculated like this there are a few numbers filled into the row already from previous code and the rest are blank.

The code working will hypothetically run through the rows in column t.

The first rows are mostly blank so it will skip those until it hits a row that has a number in it. if(df['T'].iat[x-1] != "")

once it hits the row with a number in it the script will check the logical (if the cell in the row next to it is greater than the cell above it)(if

df['PL'].iat[x] > df['PL'].iat[x-1]

if that logical is true it will print the cell from next to it over into the column. df['T'].iat[x] = df['PL'].iat[x] and if the logical is false then the cell stays blank

I think the real issue is how it iterates after that step. Right now I think its going back and just checking the last if statement again but I want it to iterate through all of them for each row.

It should read the column above to see if it is blank or filled in and then go through the if statements again only printing a number when all are met.

I have tried multiple arrangements as well as using and/or statements. I just can't seem to get it to print the right results.




import pandas as pd
import csv

df=pd.read_csv(filename)

len1 = len(df)

for x in range(len3): 

       if (df['T'].iat[x-1] != ""):

                if(df['T'].iat[x] == ""):

                    if (df['PL'].iat[x] > df['PL'].iat[x-1]):  

                        df['T'].iat[x] = df['PL'].iat[x]

The code displays no error messages however it is not printing the right results. Please let me know if I can explain it any better or if anyone has any ideas. Thanks!

3 Answers

0
Alon Kagan On

What is df? if df is a dictionary like object of 2 elements, T and PL, then len(df) will return 2.

perhaps you have meant len(df["T"]) or len(df["PL"])?

0
Community On

Your first if statement doesn't skip rows until it finds an empty cell - your second if statement does that. Your first if statement (I think) will cause this to fail when you check the very first row.

Try removing the first if. If that doesn't work, try removing the other ifs too, and see if you are able to set any values at all with df['T'].iat[x] = df['PL'].iat[x] - it may be that pandas has a separate method to set values than to get them.

0
Ben Pap On
df.loc[((df['T'] > df['T'].shift(-1)) & (df['T'] != df['PL'])), 'PL'] = df['T']

IIUC, this should do what you want.