dataframe affectation with selection

1

I got dataframe with various data :

      tag_id                    time  AA       AA_id  RSSI       p       t  status    vs  unitag  delta_time
0   15729759 2020-11-18 09:18:15.473  AA  0x00022646   -28  186000  290.15       0  2.82  Unitag         NaN
1   15729759 2020-11-18 09:18:16.039  _B  0x00000000     0  186000  290.15       0  2.82  Unitag         NaN
2   15729724 2020-11-18 09:18:31.069  AA  0x00022646   -31  187000  291.15       0  2.81  Unitag         NaN
3   15729724 2020-11-18 09:18:31.621  _B  0x00000000     0  187000  291.15       0  2.81  Unitag         NaN
4   14946100 2020-11-18 09:18:45.304  AA  0x00022646   -22  190000  290.15       0  2.81  Unitag         NaN
5   14946100 2020-11-18 09:18:45.867  _B  0x00000000     0  190000  290.15       0  2.81  Unitag         NaN
6   14945509 2020-11-18 09:19:01.916  AA  0x00022646   -30  189000  291.15       0  2.81  Unitag         NaN
7   14945509 2020-11-18 09:19:02.466  _B  0x00000000     0  189000  291.15       0  2.81  Unitag         NaN
8   15729736 2020-11-18 09:19:07.454  AA  0x00022646   -28  186000  290.15       0  2.82  Unitag         NaN
9   15729736 2020-11-18 09:19:07.995  _B  0x00000000     0  186000  290.15       0  2.82  Unitag         NaN
10  15729762 2020-11-18 09:19:08.672  AA  0x00022646   -25  185000  290.15       0  2.83  Unitag         NaN
11  15729762 2020-11-18 09:19:09.242  _B  0x00000000     0  185000  290.15       0  2.83  Unitag         NaN
12  15729759 2020-11-18 09:19:17.512  AA  0x00022646   -28  186000  290.15       0  2.82  Unitag         NaN
13  15729759 2020-11-18 09:19:18.069  _B  0x00000000     0  186000  290.15       0  2.82  Unitag         NaN
14  15729724 2020-11-18 09:19:34.535  AA  0x00022646   -32  186000  291.15       0  2.81  Unitag         NaN
15  15729724 2020-11-18 09:19:35.085  _B  0x00000000     0  186000  291.15       0  2.81  Unitag         NaN
16  14946100 2020-11-18 09:19:48.615  AA  0x00022646   -22  189000  290.15       0  2.81  Unitag         NaN
17  14946100 2020-11-18 09:19:49.206  _B  0x00000000     0  189000  290.15       0  2.81  Unitag         NaN
18  14945509 2020-11-18 09:20:04.857  AA  0x00022646   -30  189000  291.15       0  2.81  Unitag         NaN
19  14945509 2020-11-18 09:20:05.415  _B  0x00000000     0  189000  291.15       0  2.81  Unitag         NaN

And 1 empty columns that I would like to compute : delta_time . I would like to do a simple time - time.shift() but by selecting the rows that corresponds to the same tag_id and the same AA

so I tried this :

index_d = {}
for tag in df.tag_id.unique():
    index_d[tag] = df.loc[df['tag_id']==tag].index

for antenna_type in df.AA.unique():
    for tag in df.tag_id.unique():
        df.loc[df['tag_id']==tag].loc[df['AA']==antenna_type]['delta_time'] = df.iloc[index_d[tag]].loc[df['AA']==antenna_type].time - df.iloc[index_d[tag]].loc[df['AA']==antenna_type].time.shift()

but my delta_time column stays empty... any idea how to do that ??

python
dataframe
asked on Stack Overflow Nov 23, 2020 by testadaz

1 Answer

0

You could use shift inside a group, so you do not have to filter for the same antenna type:

df['prev_time'] = df.groupby('AA')['time'].shift()
answered on Stack Overflow Nov 23, 2020 by Andreas

User contributions licensed under CC BY-SA 3.0