简体   繁体   English

在 DataFrame 中查找未被其他 DataFrames 列中的值中断的连续列值

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

I have two DataFrames as follows:我有两个 DataFrame,如下所示:

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 .我想用不中断的连续 id 但df2中的 id 填充 DataFrame df1

The output should be as follows: output应该如下:

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.请注意,id 4 和 5 被跳过,因为df2的 id 4 中断了此处的连续流。

Just make a loop holding a boolean, maybe not as fast as you want but it does the trick只需制作一个装有 boolean 的循环,可能没有你想要的那么快,但它可以解决问题

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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pandas:通过将 2 列值(复合键)匹配到 1 列和另一个 dataframe 的列标签/索引来映射 2 个数据帧之间的值 - Pandas: Mapping values between 2 dataframes by matching 2 columns values (composite key) to 1 column and the column labels/index of another dataframe 将数据框列表中的值与另一个数据框列进行比较 - Comparing values in a list of dataframes to another dataframe column 使用来自另一个 dataframe 的具有特定列值的行创建一个新的数据框 - create a new dataframes with rows from another dataframe with specific columns values 根据另一个 dataframe 中的值将 dataframe 拆分为 6 个数据帧 - Split dataframe into 6 dataframes based on values in another dataframe 使用Pnadas数据帧中的其他列值有条件地替换列中的值 - Conditional replace of values in a column with another columns values in Pnadas dataframe Pandas DataFrames:如何根据另一个数据帧列中的值使用现有数据帧中的索引值定位行? - Pandas DataFrames: How to locate rows using index values in existing dataframe based on values from another dataframe column? 如何使用其他数据框列的值转换数据框的列值 - How to transform a column value of a dataframe with values of another dataframe columns 根据另一个数据帧的列值的条件将数据添加到数据帧中的列 - Adding data to columns in a dataframe based on condition on column values of another dataframe 将 dataframe 的多个列的值复制到另一个 dataframe 的 1 列中 - Copy the values of multiple columns of a dataframe into 1 column of another dataframe 根据另一个 dataframe 中的列值创建 dataframe 列 - Create a dataframe column based on values that are columns in another dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM