The pandas.get_dummies() function

This function is used to convert a categorical variable into an indicator DataFrame, which is essentially a truth table of possible values of the categorical variable. An example of this is the following command:

In [408]: melted=melt(USIndexDataDF[:2], id_vars=['TradingDate'], var_name='Index Name', value_name='Index Value')  

melted
Out[408]: TradingDate Index Name Index Value 0 2014/01/30 Nasdaq 4123.13 1 2014/01/31 Nasdaq 4103.88 2 2014/01/30 S&P 500 1794.19 3 2014/01/31 S&P 500 1782.59 4 2014/01/30 Russell 2000 1139.36 5 2014/01/31 Russell 2000 1130.88 6 2014/01/30 DJIA 15848.61 7 2014/01/31 DJIA 15698.85 In [413]: pd.get_dummies(melted['Index Name'])
Out[413]: DJIA Nasdaq Russell 2000 S&P 500 0 0 1 0 0 1 0 1 0 0 2 0 0 0 1 3 0 0 0 1 4 0 0 1 0 5 0 0 1 0 6 1 0 0 0 7 1 0 0 0

The source of the preceding data is http://vincentarelbundock.github.io/Rdatasets/csv/datasets/PlantGrowth.csv.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset