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 :
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 :
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