Advertisement
Micko81

Untitled

Jun 25th, 2017
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import pandas as pd
  4.  
  5. def getFullPath(x):
  6.   ret = ""
  7.   for i in range(1,151):
  8.     ret += getPath(x['cola' + str(i)], x['colb' + str(i)], x['colc' + str(i)])
  9.   return ret
  10.  
  11. def getPath(a,b,c):
  12.   if b == 'ret1':
  13.     return 'A'
  14.   elif b == 'ret2':
  15.     if c.startswith('B_'):
  16.       return 'B'
  17.     elif c.startswith('C_'):
  18.       return 'C'
  19.     elif c.startswith('A_'):
  20.       if a.startswith('D_'):
  21.         return 'D'
  22.       else:
  23.         return 'E'
  24.   else:
  25.     return 'F'
  26.  
  27. series = {
  28.   'cola1': pd.Series(['D_1','C_1','E_1'],index=[1,2,3]),
  29.   'colb1': pd.Series(['ret1','ret1','ret2'],index=[1,2,3]),
  30.   'colc1': pd.Series(['B_1','C_2','B_3'],index=[1,2,3]),
  31.   'cola2': pd.Series(['D_1','C_1','E_1'],index=[1,2,3]),
  32.   'colb2': pd.Series(['ret3','ret1','ret2'],index=[1,2,3]),
  33.   'colc2': pd.Series(['B_2','A_1','A_3'],index=[1,2,3]),
  34.   'cola3': pd.Series(['D_1','C_1','E_1'],index=[1,2,3]),
  35.   'colb3': pd.Series(['ret2','ret2','ret1'],index=[1,2,3]),
  36.   'colc3': pd.Series(['A_1','B_2','C_3'],index=[1,2,3]),
  37. }
  38.  
  39. df = pd.DataFrame(series, index=[1,2,3], columns=['cola1','colb1','colc1','cola2','colb2','colc2','cola3','colb3','colc3'])
  40. df['Path'] = df.apply(lambda x: getFullPath(x), axis=1)
  41.  
  42. print(df)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement