furas

pandas - ???

Jul 20th, 2018
115
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pandas as pd
  2. from io import StringIO
  3.  
  4.  
  5. def unnest(df, col, col2,reset_index=False):
  6.    
  7.    col_flat = pd.DataFrame([[i, x]
  8.                       for i, y in df[col].apply(list).iteritems()
  9.                           for x in y ], columns=['I', col]
  10.                            )
  11.    
  12.    print(col_flat)
  13.    col_flat = col_flat.set_index('I')
  14.    df = df.drop(col, 1)
  15.    df = df.merge(col_flat, left_index=True, right_index=True)
  16.  
  17.    if reset_index:
  18.        df = df.reset_index(drop=True)
  19.        
  20.    merchant_product_code = (df['Product code'] + '-' + df[col])
  21.    df['item_group_id'] = df['Product code']
  22.    df['Product code'] = merchant_product_code
  23.    
  24.    return df
  25.  
  26.  
  27. text = '''Product code,Options
  28. COWZGH,"['Newborn (2.5-6kg)', 'Big Baby (6-9kg)']"
  29. PHBC,[]
  30. ORGWBT0,"['Newborn (2.5-6kg)', 'Big Baby (6-9kg)', 'Preemie (1.5-2.5kg)']"
  31. COVWDBC,"['Newborn (2.5-6kg)', 'Big Baby (6-9kg)']"
  32. COVWMV,"['Newborn (2.5-6kg)', 'Big Baby (6-9kg)']"
  33. BZ-168,[]
  34. ORWNG,"['Newborn (2.5-6kg)', 'Big Baby (6-9kg)']"
  35. ORWNB13,"['Newborn (2.5-6kg)', 'Big Baby (6-9kg)']"
  36. HBCDTG,[]'''
  37.  
  38. # read data from text
  39. fd = StringIO(text)
  40. df = pd.read_csv(fd)
  41.  
  42. # convert strings to list
  43.  
  44. # version A - create empty lists []
  45. #df['Options'] = df['Options'].apply(eval)
  46.  
  47. # version B - create lists with empty string ['']
  48. df['Options'] = df['Options'].apply(lambda x: eval(x) if eval(x) else [''] )
  49.  
  50. # print type for every row
  51. df['Options'].apply(lambda x:print(type(x), x))
  52.  
  53. df2 = unnest(df, 'Options', 'x')
  54.  
  55. print(df2)
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×