简体   繁体   中英

Finding continuous column values in a DataFrame that are not interrupted by values in another DataFrames columns

I have two DataFrames as follows:

df1 = pd.DataFrame({ 
     'id': [1, 3, 6, 9],
     'value' : ['x']*4}) 

Out[1]:

       id   value
       1    x
       3    x
       6    x
       9    x 

df2 = pd.DataFrame({ 
    'id': [4, 10, 12],       
    'value': ['x']*3})

Out[2]:

       id   value
       4    x
       10   x
       12   x 

I want to fill DataFrame df1 with continuous ids that are not interrupted but the ids in df2 .

The output should be as follows:

Out[3]:

       id   value
       1    x
       2    Nan
       3    x
       6    x
       7    Nan
       8    Nan
       9    x       

Note that id 4 and 5 are skipped because df2 have id 4 that interrupt the continuous flow here.

Just make a loop holding a boolean, maybe not as fast as you want but it does the trick

a = [1,3,6,9]
b = [4,10,12]

add_id = True
result = []
for i in range(a[0], a[-1] + 1):
    if i in a:
        add_id = True
    if i in b:
        add_id = False
    if add_id:
        result.append(i)

output: [1, 2, 3, 6, 7, 8, 9]

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