Advertisement
furas

Pandas - changing columns

Jul 22nd, 2018
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. import pandas as pd
  2.  
  3. df = pd.DataFrame({
  4.    'Options': [ [], ["HELLO"], [], ["WORLD", "!!!"] ],
  5.    'Product code': [ "A", "B", "C", "D" ]
  6. })
  7.  
  8. def unnest(df, col, col2,reset_index=False):
  9.    for item in df[col]:
  10.         if len(item) == 0:
  11.             item.append('[]')
  12.  
  13.    col_flat = pd.DataFrame([[i, x]
  14.                       for i, y in df[col].apply(list).iteritems()
  15.                           for x in y ], columns=['I', col]
  16.                            )
  17.  
  18.    col_flat = col_flat.set_index('I')
  19.  
  20.    df = df.drop(col, 1)
  21.    df = df.merge(col_flat, left_index=True, right_index=True)
  22.  
  23.    if reset_index:
  24.        df = df.reset_index(drop=True)
  25.  
  26.    df['item_group_id'] = df['Product code']
  27.    
  28.    df['Product code'] += df[col].apply(lambda val: '' if val == '[]' else '-' + val)
  29.  
  30.    return df
  31.  
  32. print(df)
  33.  
  34. df = unnest(df,'Options','product code')
  35.  
  36. print(df)
  37.  
  38. # before
  39. '''
  40.        Options Product code
  41. 0            []            A
  42. 1       [HELLO]            B
  43. 2            []            C
  44. 3  [WORLD, !!!]            D
  45. '''
  46.  
  47. # after
  48.  
  49. '''
  50.  Product code Options item_group_id
  51. 0            A      []             A
  52. 1      B-HELLO   HELLO             B
  53. 2            C      []             C
  54. 3      D-WORLD   WORLD             D
  55. 3        D-!!!     !!!             D
  56. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement