Pandas Looking At Next Row And Swapping Values
If i see a A&T consecutively, I will set found=True for A. set remove=True for T. Set value of T as A (copy A's value to T) Set T found=True If I see G&C consecutively,
Solution 1:
I would create some temporary columns to track the lagged flags and values and next flags. Then you can compare directly:
df['prior_flag'] = df.Flag.shift()
df['next_flag'] = df.Flag.shift(-1)
df['prior_value'] = df.Value.shift()
# Checkfor'A' followed by'T'
df.loc[(df.Flag =='A') & (df.next_flag =='T'), 'found'] =True
df.loc[(df.Flag =='T') & (df.prior_flag =='A'), 'remove'] =True
df.loc[(df.Flag =='T') & (df.prior_flag =='A'), 'Value'] = \
df.loc[(df.Flag =='T') & (df.prior_flag =='A'), 'prior_value']
# Checkfor'G' followed by'C'
df.loc[(df.Flag =='G') & (df.next_flag =='C'), 'found'] =True
df.loc[(df.Flag =='C') & (df.prior_flag =='G'), 'remove'] =True
temp = df.loc[(df.Flag =='G') & (df.next_flag =='C'), 'Value'].values
df.loc[(df.Flag =='G') & (df.next_flag =='C'), 'Value'] = \
df.loc[(df.Flag =='C') & (df.prior_flag =='G'), 'Value'].values
df.loc[(df.Flag =='C') & (df.prior_flag =='G'), 'Value'] = temp
df.drop(['next_flag', 'prior_flag', 'prior_value'], axis=1, inplace=True)
>>> df
Flag Value found remove
0 A 3TrueFalse1 T 3FalseTrue2 A 3FalseFalse3 A 4FalseFalse4 G 4TrueFalse5 C 3FalseTrue6 T 1FalseFalse
Because you want to swap values when G is followed by C, I created a temporary variable temp
to store the intermediate copy value.
All temporary columns are then dropped at the end.
To view the remaining rows that have not been removed:
>>> df[~df.remove]
Flag Value found remove
0 A 3TrueFalse2 A 3FalseFalse3 A 4FalseFalse4 G 4TrueFalse6 T 1FalseFalse
Post a Comment for "Pandas Looking At Next Row And Swapping Values"