条件下有多个不平等的数据框



我有7个dataframes(df_1,df_2,df_3,...,df_7(,均具有相同的列,但不同的长度,但有时有相同的值。

我想在条件下加入所有7个数据范围

if df_n.iloc[row_i] != df_n+1.iloc[row_i] and df_n.iloc[row_i][0] < df_n+1.iloc[row_i][0]:
      pd.concat([df_n.iloc[row_i], df_n+1.iloc[row_i], df_n+2.iloc[row_i],
      ...., df_n+6.iloc[row_i]])

其中 df_n.iloc[row_i]是nth dataframe的ITH行,而df_n.iloc[row_i][0]是ITH行的第一列。

例如,如果我们只有2个数据范围,并且该 len(df_1)&lt;len(df_2),如果我们使用的条件 input 将是:

df_1                                    df_2
index    0      1       2               index    0        1       2
0        12.12  11.0    31              0        12.2     12.6    30
1        12.3   12.1    33              1        12.3     12.1    33
2        10     9.1     33              2        13       12.1    23
3        16     12.1    33              3        13.1     12.1    27
                                        4        14.4     13.1    27
                                        5        15.2     13.2    28

输出将是:

conditions -> pd.concat([df_1, df_2]):
index    0      1       2      3      4      5     
0        12.12  11.0    31     12.2   12.6   30
2        10     9.1     33     13     12.1   23
4        nan                   14.4   13.1   27
5        nan                   15.2   13.2   28

有一个简单的方法吗?

iiuc concat首先,列的 groupby获得了不同,我们只能实现您的条件

s=pd.concat([df1,df2],1)
s1=s.groupby(level=0,axis=1).apply(lambda x : x.iloc[:,0]-x.iloc[:,1])
yourdf=s[s1.ne(0).any(1)&s1.iloc[:,0].lt(0)|s1.iloc[:,0].isnull()]
Out[487]: 
           0     1     2     0     1   2
index                                   
0      12.12  11.0  31.0  12.2  12.6  30
2      10.00   9.1  33.0  13.0  12.1  23
4        NaN   NaN   NaN  14.4  13.1  27
5        NaN   NaN   NaN  15.2  13.2  28

最新更新