0

I need help to take a python pandas dataframe containing the below data and generate a decision tree in a text file using the first two columns to generate the nodes and the MULTIPLIER column to contain the values associated with each 'leaf'.

import pandas as pd
d = {'device': ['mobile', 'mobile', 'mobile', 'tablet', 'tablet','tablet', 
'desktop', 'desktop', 'desktop'], 'day': ['monday', 'tuesday', 'wednesday', 
'monday', 'tuesday', 'wednesday', 'monday', 'tuesday', 'wednesday'], 
'multiplier':[2.5, 3.5, 4.5, 2, 2.4, 3, 1.5, 1.5, 1.1]}
frame = pd.DataFrame(data=d, columns=['device', 'day', 'multiplier'])    
df=frame.sort_values(['device','day'], ascending=True)
df.reset_index(drop=True, inplace=True)

enter image description here

The resulting .txt file containing the decision tree needs to be in the format below:

http://textuploader.com/dqcoe

As you can see, I need to generate a 'leaf_name' which combines its component node names separated by an underscore.

To pre-empt your questions on this approach, i specifically want a recursive solution as the number of input columns in the dataframe (before the MULTIPLIER column) will vary in future, which will in turn alter the number of levels in the resultant tree.

Any help would be much appreciated.

Thank you in advance

Colin Blyth
  • 83
  • 1
  • 7

0 Answers0