I assumed this would be fairly straight-forward, but apparently I'm missing something here.
I want to be able to utilize np.where
with df.groupby('Name').apply()
to create a new column in df
(call it 'New'
), where the values of the column are 1
if the indices of the respective group (indices corresponding to the original df
) are greater than or equal to (>=
) a particular value, else 0
.
For background, I am grouping df
by the 'Name'
column and I have a dict()
that contains the corresponding value to use for each name from the groupby()
. I hope that is clear, I can provide further clarification if necessary.
Here is what I have so far, given sample df
:
df = pd.DataFrame([['William', 1, 0, 0, 0, 1],['James', 0, 1, 1, 1, 1],['James', 1, 0, 0, 0, 0],
['James', 1, 0, 1, 1, 0],['William', 0, 1, 1, 0, 1],['William', 0, 0, 0, 0, 0],
['William', 1, 0, 1, 1, 0],['James', 0, 1, 1, 0, 1],['James', 0, 0, 0, 0, 0]],
columns=['Name','x1','x2','x3','x4','Interest'])
Name x1 x2 x3 x4 Interest
0 William 1 0 0 0 1
1 James 0 1 1 1 1
2 James 1 0 0 0 0
3 James 1 0 1 1 0
4 William 0 1 1 0 1
5 William 0 0 0 0 0
6 William 1 0 1 1 0
7 James 0 1 1 0 1
8 James 0 0 0 0 0
Then I am finding the last row in df
for each group where the 'Interest'
column has a 1
, using:
mydict = df[df['Interest']==1].groupby('Name').apply(lambda x: x.index[-1]).to_dict()
{'James': 7, 'William': 4}
Note: This is a simplified example. For my actual application, I am pulling the index of the 3rd to last row (i.e. .apply(lambda x: x.index[-3]).to_dict()
), however the next part is where the root of my question lies.
Now, I want to create a new column 'Name'
, where the value is 1
if the row index is >=
the value in mydict
for that group, else 0
. I've tried a few things:
for key, val in mydict.items():
df['New'] = np.where((df['Name']==key) & (df.index>=val), 1, 0)
This obviously will override anything done for 'James'
and just return the correct column for 'William'
. How can I efficiently do this?
To be thorough, here is my expected output:
Name x1 x2 x3 x4 Interest New
0 William 1 0 0 0 1 0
1 James 0 1 1 1 1 0
2 James 1 0 0 0 0 0
3 James 1 0 1 1 0 0
4 William 0 1 1 0 1 1
5 William 0 0 0 0 0 1
6 William 1 0 1 1 0 1
7 James 0 1 1 0 1 1
8 James 0 0 0 0 0 1