[英]How to compare two pandas dataframe rows and return the value?
I have 2 pandas dataframe like 1st Datafarme我有 2 个 pandas dataframe 就像第一个 Datafarme
index a b c d
0 4 5 3 2
1 1 10 21 34
2 3 32 1 45
3 56 42 42 23
2nd Dataframe 2号 Dataframe
index a b c d
LL 2 2 2 1
UL 42 2 1 8
I want excepted output like this.我想要这样的例外 output 。
index a b c d result
0 4 5 3 2 [5,3]
1 1 10 21 34 [1,10,21,34]
2 3 32 1 45 [32,1,45]
3 56 42 42 23 [56,42,42,23]
In this I m comparing 2nd dataframe rows with 1st dataframe row.在此,我将第 2 行 dataframe 行与第 1 行 dataframe 行进行比较。 Condition is like if 1st dataframe value is smaller than LL row of 2nd dataframe it will be add in result column likewise if 1st dataframe value is greater than UL row of 2nd dataframe it will be added in result column. Condition is like if 1st dataframe value is smaller than LL row of 2nd dataframe it will be add in result column likewise if 1st dataframe value is greater than UL row of 2nd dataframe it will be added in result column.
you could do你可以做
cond = (first_df<second_df.loc['LL',:]) | (first_df>second_df.loc['UL',:])
first_df['result'] = first_df[cond].apply(lambda x : list(x.dropna()), axis=1)
this being given that index
is an index in both your dataframes, if it's not, you could do鉴于index
是您的两个数据框中的索引,如果不是,您可以这样做
first_df.set_index('index', inplace=True)
second_df.set_index('index', inplace=True)
a b c d result
index
0 4 5 3 2 [5.0, 3.0]
1 1 10 21 34 [1.0, 10.0, 21.0, 34.0]
2 3 32 1 45 [32.0, 1.0, 45.0]
3 56 42 42 23 [56.0, 42.0, 42.0, 23.0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.