简体   繁体   中英

Pandas dataframe: propagate True values if timestamp is identical

Best described by an example. Input is

   ts    val
0   10  False
1   20   True
2   20  False
3   30   True
4   40  False
5   40  False
6   40  False
7   60   True
8   60  False

desired output is

   ts    val
0   10  False
1   20   True
2   20   True
3   30   True
4   40  False
5   40  False
6   40  False
7   60   True
8   60  True

The idea is as follows: if we see at least one True value inside the same ts cluster(ie same ts value), make all other values True that have the exact same timestamp.

You can use groupby on column 'ts', and then apply using .any() to determine whether any of val is True in the cluster/group.

import pandas as pd

# your data
# =====================
print(df)

Out[58]: 
   ts    val    data
0  10  False  0.3332
1  20   True -0.6877
2  20  False -0.6004
3  30   True  0.1922
4  40  False  0.2472
5  40  False -0.0117
6  40  False  0.8607
7  60   True -1.1464
8  60  False  0.0698


# processing
# =====================
# as suggested by @DSM, transform is best way to do it
df['val'] = df.groupby('ts')['val'].transform(any)

Out[61]: 
   ts    val    data
0  10  False  0.3332
1  20   True -0.6877
2  20   True -0.6004
3  30   True  0.1922
4  40  False  0.2472
5  40  False -0.0117
6  40  False  0.8607
7  60   True -1.1464
8  60   True  0.0698

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM