Guest User

Untitled

a guest
Feb 24th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. # Performing a Flatmap Operation on a selected column
  2. # containing rows of lists this example uses
  3. # Pythons itertools
  4. import pandas as pd
  5. from itertools import chain
  6.  
  7.  
  8. def flatmap(f, items):
  9. return list(chain.from_iterable(list(map(f, items))))
  10.  
  11.  
  12. def create_dict(df, headers_ls, f_var, x):
  13. temp_dict = dict({})
  14. for h in headers_ls:
  15. temp_dict[h] = df[h]
  16. temp_dict[f_var] = x
  17. return temp_dict
  18.  
  19.  
  20. def flatmap_df(df, flatten_col):
  21. headers_ls = df.columns
  22. rec_dict = df[headers_ls].to_dict(orient='records')
  23. return pd.DataFrame(flatmap(lambda rec_dict: [create_dict(rec_dict,
  24. headers_ls, flatten_col,
  25. col_ls) for col_ls in rec_dict[flatten_col]], rec_dict))
  26.  
  27.  
  28. ##########
  29. # Create a DataFrame with a row list
  30. ##########
  31. print("\nOriginal DataFrame:")
  32. episodes = [
  33. {"id": 1, "topics": [5,7,8], "date": '2018-01-01'},
  34. {"id": 2, "topics": [65,43,64], "date": '2018-01-02'}
  35. ]
  36. df_epi = pd.DataFrame(episodes)
  37. print(df_epi)
  38. print("\nFlattened DataFrame:")
  39. ##########
  40. # Flatten the column lists
  41. ##########
  42. flattened_df = flatmap_df(df_epi[['id', 'topics']], 'topics')
  43. print(flattened_df)
Add Comment
Please, Sign In to add comment