Skip to content

Instantly share code, notes, and snippets.

@euri10
Created October 12, 2015 09:13
Show Gist options
  • Save euri10/40ef83fc808415ad3523 to your computer and use it in GitHub Desktop.
Save euri10/40ef83fc808415ad3523 to your computer and use it in GitHub Desktop.
import pandas as pd
from timeit import Timer
def euri10(df):
df['t'] = (df['Matrix'].str.endswith('A') & df['Matrix'].shift(-1).str.endswith('B')) | (
df['Matrix'].str.endswith('B') & df['Matrix'].shift(1).str.endswith('A'))
df['p'] = (df['Matrix'].str[:-1] == df['Matrix'].shift(-1).str[:-1]) | (
df['Matrix'].str[:-1] == df['Matrix'].shift(1).str[:-1])
df['e'] = df['p'] & df['t']
final = df[df['e']]
return final
def rurp(df):
df['ShiftUp'] = df['Matrix'].shift(-1)
df['ShiftDown'] = df['Matrix'].shift()
def check_matrix(x):
if pd.isnull(x.ShiftUp) == False and isinstance(x.Matrix, str) and x.Matrix[:-1] == x.ShiftUp[:-1]:
return True
elif pd.isnull(x.ShiftDown) == False and isinstance(x.Matrix, str) and x.Matrix[:-1] == x.ShiftDown[:-1]:
return True
else:
return False
df['new'] = df.apply(check_matrix, axis=1)
final = df.drop(['ShiftUp', 'ShiftDown'], axis=1)
return final
if __name__ == '__main__':
df = pd.read_excel(r'C:\Users\bbarthelet\Dropbox\DB.xls')
n = 100
t_rurp = Timer(lambda: rurp(df))
print t_rurp.timeit(number=n)
t_euri10 = Timer(lambda: euri10(df))
print t_euri10.timeit(number=n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment