Created
October 12, 2015 09:13
-
-
Save euri10/40ef83fc808415ad3523 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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