创建示例数据

To create a dataframe from every combination of some given values, like R’s expand.grid() function, we can create a dict where the keys are column names and the values are lists of the data values:

  1. In [195]: def expand_grid(data_dict):
  2. .....: rows = itertools.product(*data_dict.values())
  3. .....: return pd.DataFrame.from_records(rows, columns=data_dict.keys())
  4. .....:
  5. In [196]: df = expand_grid(
  6. .....: {'height': [60, 70],
  7. .....: 'weight': [100, 140, 180],
  8. .....: 'sex': ['Male', 'Female']})
  9. .....:
  10. In [197]: df
  11. Out[197]:
  12. height weight sex
  13. 0 60 100 Male
  14. 1 60 100 Female
  15. 2 60 140 Male
  16. 3 60 140 Female
  17. 4 60 180 Male
  18. 5 60 180 Female
  19. 6 70 100 Male
  20. 7 70 100 Female
  21. 8 70 140 Male
  22. 9 70 140 Female
  23. 10 70 180 Male
  24. 11 70 180 Female