-1

In my dataset, I want to create dummy variables for the variable "eps". The condition should be something like this, if the eps of certain company is more than 10 euro, I want to assign 0 otherwise 1 for everything else.

Here's the sample image :

enter image description here

tlentali
  • 3,407
  • 2
  • 14
  • 21
Asif Zahir
  • 15
  • 2
  • I strongly advise you to have a read here about [how to ask a question](https://stackoverflow.com/help/how-to-ask) on stackoverflow. In any case, IIUC you are looking to populate that 'dummies' column with 1's or 0's depending whether the EPS is greater than 10. If that, you can try the following: ```import numpy as np```, and then create that column ```df['dummies'] = np.where(df['EPS']>10,1,0)``` – sophocles Dec 20 '21 at 10:30
  • Thank you very much. Yes, I'll read that. – Asif Zahir Dec 20 '21 at 10:53

1 Answers1

0

We can start by using numpy.where to assign a boolean value in a new column :

>>> import numpy as np

>>> df["Dummies"] = np.where(df["Eps"] > 10, 1, 0)
>>> df
    stock_name  Eps Dummies
0   Apple       20  1
1   Tesla       25  1
2   Berkshire   10  0

Then we can even use a pivot_table to get a dummies matrix format :

>>> pd.pivot_table(df, values='Dummies', columns=['stock_name'], aggfunc=np.min)
stock_name   Apple  Berkshire   Tesla
Dummies      1      0           1
tlentali
  • 3,407
  • 2
  • 14
  • 21