0

I want to convert the column 1 with [i-1], if column 2 is False, iterated row by row. How can i do this in efficient way without using the for loop, example of for loop can be seen below:

import pandas as pd 

data_lamination = pd.DataFrame()
data_lamination['loss'] = [0.1,0.2,0.3,0.4,0.5]
data_lamination['nominal_wall_thickness'] = 1
data_lamination['flaw_distance'] = [10,0.2,0.3,0.4,0.01]
data_lamination['t_c'] = [0.1,0.01,0.3,0.01,0.5]
data_lamination['FCA'] = 0.1
data_lamination['lamination_distance_condition'] = data_lamination['flaw_distance'] >= 2 * data_lamination['t_c'] 

data_lamination['lamination_length']=[0.1,0.2,0.3,0.4,0.5]
data_lamination['lamination_depth']=[0.1,0.2,0.3,0.4,0.5]
    

new_value_length = []
new_value_depth = []

def abc(data_lamination, *args, **kwargs):
    

    for i, row in data_lamination.iterrows():
        if data_lamination.loc[i,'lamination_distance_condition'] == False:
        
            A = data_lamination.loc[i,'lamination_length'] + data_lamination.loc[i - 1,'lamination_length']
            B = data_lamination.loc[i,'lamination_depth'] + data_lamination.loc[i - 1,'lamination_depth']
            
        else:
             A = data_lamination.loc[i,'lamination_length']
             B = data_lamination.loc[i,'lamination_depth']
    
        new_value_length.append(A)
        new_value_depth.append(B)                    
    
    return A,B



B = abc(data_lamination)

data_lamination['lamination_length_organised'] = new_value_length 
data_lamination['lamination_depth_organised'] = new_value_depth 

Your help will be much appriciated.

Thank you in advance.

2
  • Please add a simple example of input and output Commented Nov 20, 2020 at 8:54
  • @TomRon updated with all the required data with working for loop. Thanks Commented Nov 20, 2020 at 9:09

1 Answer 1

1

you can define something like this

df['sum'] = df[['lamination_length']].apply(lambda x: [0] + [x.loc[i] + x.loc[i - 1] for i in range(1, len(x))])
df.loc[df['lamination_distance_condition'], 'new_col'] = df[df['lamination_distance_condition']]['lamination_length'].tolist()
df.loc[~df['lamination_distance_condition'], 'new_col'] = df[~df['lamination_distance_condition']]['sum'].tolist()

df is your data_lamination. So you define a temporary column that is length[i - 1] + length[i] (first element set to 0). if the condition is true, take from the old column, if false, take from the new one.

Edit: Maybe the final tolist() can be removed. I did not check.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.