Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14508

Add/fill pandas column based on range in rows from another dataframe

$
0
0

Working with pandas, I have df1 indexed by time samples:

data = '''\time       flags    input                  8228835.0  53153.0  32768.08228837.0  53153.0  32768.08228839.0  53153.0  32768.08228841.0  53153.0  32768.08228843.0  61345.0  32768.0'''fileobj = pd.compat.StringIO(data)df1 = pd.read_csv(fileobj, sep='\s+', index_col='time')

df2 indicates time ranges with start and end to define ranges where the state of 'check' is True:

data = '''\        check     start       end20536   True   8228837   822899320576   True   8232747   823286920554   True   8230621   823076120520   True   8227351   822750720480   True   8223549   822366920471   True   8221391   8221553'''fileobj = pd.compat.StringIO(data)df2 = pd.read_csv(fileobj, sep='\s+')

What I need to do is add a column for 'check' to df1 and fill out the actual time ranges defined in df2 with the value of True. All others should be False. An example result would be:

             flags    input    checktime                       8228835.0  53153.0  32768.0    False8228837.0  53153.0  32768.0    True8228839.0  53153.0  32768.0    True8228841.0  53153.0  32768.0    True8228843.0  61345.0  32768.0    True....8228994.0. 12424.0. 32768.0.   False

Viewing all articles
Browse latest Browse all 14508

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>