Changing a column value in pandas dataframe excluding the tail in group by
up vote
2
down vote
favorite
Let's take an example of a python dataframe.
ID Age Bp
1 22 1
1 22 1
1 22 0
1 22 1
2 21 0
2 21 1
2 21 0
In the above code, the last n series for column BP (lets consider n to be 2) with group by ID should be excluded and the rest of the BP should be changed to 0. I have tried it with tail but it does not work.
It should look like this.
ID Age BP
1 22 0
1 22 0
1 22 0
1 22 1
2 21 0
2 21 1
2 21 0
python pandas
add a comment |
up vote
2
down vote
favorite
Let's take an example of a python dataframe.
ID Age Bp
1 22 1
1 22 1
1 22 0
1 22 1
2 21 0
2 21 1
2 21 0
In the above code, the last n series for column BP (lets consider n to be 2) with group by ID should be excluded and the rest of the BP should be changed to 0. I have tried it with tail but it does not work.
It should look like this.
ID Age BP
1 22 0
1 22 0
1 22 0
1 22 1
2 21 0
2 21 1
2 21 0
python pandas
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Let's take an example of a python dataframe.
ID Age Bp
1 22 1
1 22 1
1 22 0
1 22 1
2 21 0
2 21 1
2 21 0
In the above code, the last n series for column BP (lets consider n to be 2) with group by ID should be excluded and the rest of the BP should be changed to 0. I have tried it with tail but it does not work.
It should look like this.
ID Age BP
1 22 0
1 22 0
1 22 0
1 22 1
2 21 0
2 21 1
2 21 0
python pandas
Let's take an example of a python dataframe.
ID Age Bp
1 22 1
1 22 1
1 22 0
1 22 1
2 21 0
2 21 1
2 21 0
In the above code, the last n series for column BP (lets consider n to be 2) with group by ID should be excluded and the rest of the BP should be changed to 0. I have tried it with tail but it does not work.
It should look like this.
ID Age BP
1 22 0
1 22 0
1 22 0
1 22 1
2 21 0
2 21 1
2 21 0
python pandas
python pandas
asked Nov 10 at 17:57
user123
185
185
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Use cumcount
with ascending=False
for counter from back per groups and assign 0
with numpy.where
:
n = 2
mask = df.groupby('ID').cumcount(ascending=False) < n
df['Bp'] = np.where(mask, df['Bp'], 0)
Alternatives:
df.loc[~mask, 'Bp'] = 0
df['Bp'] = df['Bp'].where(mask, 0)
print (df)
ID Age Bp
0 1 22 0
1 1 22 0
2 1 22 0
3 1 22 1
4 2 21 0
5 2 21 1
6 2 21 0
Details:
print (df.groupby('ID').cumcount(ascending=False))
0 3
1 2
2 1
3 0
4 2
5 1
6 0
dtype: int64
print (mask)
0 False
1 False
2 True
3 True
4 False
5 True
6 True
dtype: bool
Thanks for the help it worked
– user123
Nov 10 at 19:30
@user123 - You are welcome! If my answer was helpful, don't forget accept it. To mark an answer as accepted, click on the check mark beside the answer to toggle it from hollow to green (see screenshot). Thank you.
– jezrael
Nov 10 at 19:30
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Use cumcount
with ascending=False
for counter from back per groups and assign 0
with numpy.where
:
n = 2
mask = df.groupby('ID').cumcount(ascending=False) < n
df['Bp'] = np.where(mask, df['Bp'], 0)
Alternatives:
df.loc[~mask, 'Bp'] = 0
df['Bp'] = df['Bp'].where(mask, 0)
print (df)
ID Age Bp
0 1 22 0
1 1 22 0
2 1 22 0
3 1 22 1
4 2 21 0
5 2 21 1
6 2 21 0
Details:
print (df.groupby('ID').cumcount(ascending=False))
0 3
1 2
2 1
3 0
4 2
5 1
6 0
dtype: int64
print (mask)
0 False
1 False
2 True
3 True
4 False
5 True
6 True
dtype: bool
Thanks for the help it worked
– user123
Nov 10 at 19:30
@user123 - You are welcome! If my answer was helpful, don't forget accept it. To mark an answer as accepted, click on the check mark beside the answer to toggle it from hollow to green (see screenshot). Thank you.
– jezrael
Nov 10 at 19:30
add a comment |
up vote
2
down vote
accepted
Use cumcount
with ascending=False
for counter from back per groups and assign 0
with numpy.where
:
n = 2
mask = df.groupby('ID').cumcount(ascending=False) < n
df['Bp'] = np.where(mask, df['Bp'], 0)
Alternatives:
df.loc[~mask, 'Bp'] = 0
df['Bp'] = df['Bp'].where(mask, 0)
print (df)
ID Age Bp
0 1 22 0
1 1 22 0
2 1 22 0
3 1 22 1
4 2 21 0
5 2 21 1
6 2 21 0
Details:
print (df.groupby('ID').cumcount(ascending=False))
0 3
1 2
2 1
3 0
4 2
5 1
6 0
dtype: int64
print (mask)
0 False
1 False
2 True
3 True
4 False
5 True
6 True
dtype: bool
Thanks for the help it worked
– user123
Nov 10 at 19:30
@user123 - You are welcome! If my answer was helpful, don't forget accept it. To mark an answer as accepted, click on the check mark beside the answer to toggle it from hollow to green (see screenshot). Thank you.
– jezrael
Nov 10 at 19:30
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Use cumcount
with ascending=False
for counter from back per groups and assign 0
with numpy.where
:
n = 2
mask = df.groupby('ID').cumcount(ascending=False) < n
df['Bp'] = np.where(mask, df['Bp'], 0)
Alternatives:
df.loc[~mask, 'Bp'] = 0
df['Bp'] = df['Bp'].where(mask, 0)
print (df)
ID Age Bp
0 1 22 0
1 1 22 0
2 1 22 0
3 1 22 1
4 2 21 0
5 2 21 1
6 2 21 0
Details:
print (df.groupby('ID').cumcount(ascending=False))
0 3
1 2
2 1
3 0
4 2
5 1
6 0
dtype: int64
print (mask)
0 False
1 False
2 True
3 True
4 False
5 True
6 True
dtype: bool
Use cumcount
with ascending=False
for counter from back per groups and assign 0
with numpy.where
:
n = 2
mask = df.groupby('ID').cumcount(ascending=False) < n
df['Bp'] = np.where(mask, df['Bp'], 0)
Alternatives:
df.loc[~mask, 'Bp'] = 0
df['Bp'] = df['Bp'].where(mask, 0)
print (df)
ID Age Bp
0 1 22 0
1 1 22 0
2 1 22 0
3 1 22 1
4 2 21 0
5 2 21 1
6 2 21 0
Details:
print (df.groupby('ID').cumcount(ascending=False))
0 3
1 2
2 1
3 0
4 2
5 1
6 0
dtype: int64
print (mask)
0 False
1 False
2 True
3 True
4 False
5 True
6 True
dtype: bool
answered Nov 10 at 18:36
jezrael
307k20241316
307k20241316
Thanks for the help it worked
– user123
Nov 10 at 19:30
@user123 - You are welcome! If my answer was helpful, don't forget accept it. To mark an answer as accepted, click on the check mark beside the answer to toggle it from hollow to green (see screenshot). Thank you.
– jezrael
Nov 10 at 19:30
add a comment |
Thanks for the help it worked
– user123
Nov 10 at 19:30
@user123 - You are welcome! If my answer was helpful, don't forget accept it. To mark an answer as accepted, click on the check mark beside the answer to toggle it from hollow to green (see screenshot). Thank you.
– jezrael
Nov 10 at 19:30
Thanks for the help it worked
– user123
Nov 10 at 19:30
Thanks for the help it worked
– user123
Nov 10 at 19:30
@user123 - You are welcome! If my answer was helpful, don't forget accept it. To mark an answer as accepted, click on the check mark beside the answer to toggle it from hollow to green (see screenshot). Thank you.
– jezrael
Nov 10 at 19:30
@user123 - You are welcome! If my answer was helpful, don't forget accept it. To mark an answer as accepted, click on the check mark beside the answer to toggle it from hollow to green (see screenshot). Thank you.
– jezrael
Nov 10 at 19:30
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241848%2fchanging-a-column-value-in-pandas-dataframe-excluding-the-tail-in-group-by%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown