index-1 on itterrow makes new row at the end
Question:
Based on this table
pernr
plans
mnth
jum_mnth
123
000
1
NaN
123
001
3
NaN
123
001
6
NaN
789
002
10
NaN
789
003
2
NaN
789
003
2
NaN
789
002
2
NaN
I want to set ‘jum_mnth’ from ‘mnth’. ‘jum_mnth’ have value if:
- its last row from same plans
- last row from same pernr
so i tried:
for index, row in que.iterrows():
if row['pernr'] != nipp:
que_cop.at[index-1, 'jum_mnth'] = mon
nipp = row['pernr']
plan = row['plans']
mon = row['mnth']
else:
if row['plans'] == plan:
mon = mon + row['mnth']
else:
que_cop.at[index-1, 'jum_mnth'] = mon
print(str(nipp),plan,str(mon))
plan = row['plans']
mon = row['mnth']
if index == que_cop.index[-2]:
que_cop.at[index, 'jum_mnth'] = mon
but it resulting new row ( index -1) at the last like this:
pernr
plans
mnth
jum_mnth
123
000
1
1.0
123
001
3
NaN
123
001
6
9.0
789
002
10
10.0
789
003
2
NaN
789
003
2
4.0
789
002
2
NaN
NaN
NaN
NaN
0.0
and the last row didnt have jum_mnth (it should have jum_mnth)
expected:
pernr
plans
mnth
jum_mnth
123
000
1
1
123
001
3
NaN
123
001
6
9
789
002
10
10
789
003
2
NaN
789
003
2
4
789
002
2
2
so what happened?
any help i would appreciate it.
Answers:
You can use:
grp = (df[['pernr', 'plans']].ne(df[['pernr', 'plans']].shift())
.any(axis=1).cumsum()
)
g = df.groupby(grp)['mnth']
df['jum_mnth'] = g.transform('sum').where(g.cumcount(ascending=False).eq(0))
Output:
pernr plans mnth jum_mnth
0 123 000 1 1.0
1 123 001 3 NaN
2 123 001 6 9.0
3 789 002 10 10.0
4 789 003 2 NaN
5 789 003 2 4.0
6 789 002 2 2.0
Based on this table
pernr | plans | mnth | jum_mnth |
---|---|---|---|
123 | 000 | 1 | NaN |
123 | 001 | 3 | NaN |
123 | 001 | 6 | NaN |
789 | 002 | 10 | NaN |
789 | 003 | 2 | NaN |
789 | 003 | 2 | NaN |
789 | 002 | 2 | NaN |
I want to set ‘jum_mnth’ from ‘mnth’. ‘jum_mnth’ have value if:
- its last row from same plans
- last row from same pernr
so i tried:
for index, row in que.iterrows():
if row['pernr'] != nipp:
que_cop.at[index-1, 'jum_mnth'] = mon
nipp = row['pernr']
plan = row['plans']
mon = row['mnth']
else:
if row['plans'] == plan:
mon = mon + row['mnth']
else:
que_cop.at[index-1, 'jum_mnth'] = mon
print(str(nipp),plan,str(mon))
plan = row['plans']
mon = row['mnth']
if index == que_cop.index[-2]:
que_cop.at[index, 'jum_mnth'] = mon
but it resulting new row ( index -1) at the last like this:
pernr | plans | mnth | jum_mnth |
---|---|---|---|
123 | 000 | 1 | 1.0 |
123 | 001 | 3 | NaN |
123 | 001 | 6 | 9.0 |
789 | 002 | 10 | 10.0 |
789 | 003 | 2 | NaN |
789 | 003 | 2 | 4.0 |
789 | 002 | 2 | NaN |
NaN | NaN | NaN | 0.0 |
and the last row didnt have jum_mnth (it should have jum_mnth)
expected:
pernr | plans | mnth | jum_mnth |
---|---|---|---|
123 | 000 | 1 | 1 |
123 | 001 | 3 | NaN |
123 | 001 | 6 | 9 |
789 | 002 | 10 | 10 |
789 | 003 | 2 | NaN |
789 | 003 | 2 | 4 |
789 | 002 | 2 | 2 |
so what happened?
any help i would appreciate it.
You can use:
grp = (df[['pernr', 'plans']].ne(df[['pernr', 'plans']].shift())
.any(axis=1).cumsum()
)
g = df.groupby(grp)['mnth']
df['jum_mnth'] = g.transform('sum').where(g.cumcount(ascending=False).eq(0))
Output:
pernr plans mnth jum_mnth
0 123 000 1 1.0
1 123 001 3 NaN
2 123 001 6 9.0
3 789 002 10 10.0
4 789 003 2 NaN
5 789 003 2 4.0
6 789 002 2 2.0