Reputation: 131
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.
Upvotes: 0
Views: 40
Reputation: 274
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.
Upvotes: 1