合并多个列表并找到最大值

时间:2018-05-22 12:16:50

标签: python pandas

pandas可以组合多个读数列表并返回aoiFeatures中元素的最大读数值吗?

假设:

# FYI: 2.4 million elements in each of these lists in reality
allFeatures =  [101, 179, 181, 183, 185, 843, 845, 847, 849, 851] 
allReadings1 = [0.27, 0.25, 0.13, 0.04, 0.05, 0.09, 0.15, 0.13, 0.12, 0.20]
allReadings2 = [0.25, 0.06, 0.29, 0.29, 0.04, 0.21, 0.07, 0.06, 0.07, 0.06]
allReadings3 = [0.12, 0.02, 0.20, 0.27, 0.04, 0.08, 0.11, 0.24, 0.00, 0.13]
allReadings4 = [0.21, 0.00, 0.22, 0.11, 0.24, 0.16, 0.11, 0.18, 0.27, 0.14]
allReadings5 = [0.02, 0.18, 0.26, 0.22, 0.23, 0.15, 0.24, 0.28, 0.00, 0.07]
allReadings6 = [0.08, 0.25, 0.21, 0.23, 0.14, 0.21, 0.18, 0.09, 0.17, 0.27]
allReadings7 = [0.20, 0.02, 0.28, 0.16, 0.18, 0.27, 0.29, 0.19, 0.29, 0.13]
allReadings8 = [0.17, 0.01, 0.07, 0.23, 0.14, 0.20, 0.19, 0.01, 0.15, 0.17]
allReadings9 = [0.12, 0.18, 0.09, 0.10, 0.00, 0.03, 0.11, 0.03, 0.14, 0.14]
allReadings10 =[0.13, 0.03, 0.20, 0.13, 0.30, 0.30, 0.28, 0.12, 0.19, 0.22]

# FYI: 67,000 elements in this list in reality
aoiFeatures = [181, 843, 849]

结果:

181 0.29
843 0.27
849 0.29

3 个答案:

答案 0 :(得分:2)

首先<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="results" style="height: 460px; overflow-y: auto;"> <div class="resdrg"><div class="item asp_result_pagepost asp_an_fadeIn"> <div class="asp_content"> <a class="asp_res_image_url" href="https://einsteam.com/contact/"> <div class="asp_image" style="background-image: url(&quot;http://einsteam.com/#contactus&quot;);"> <div class="void"></div> </div> </a> <h3><a class="asp_res_url" href="https://einsteam.com/contact/"> Real Estate <span class="overlap"></span> </a></h3> <div class="etc"> </div> </div> <div class="clear"></div> </div> <div class="asp_spacer"></div><div class="item asp_result_pagepost asp_an_fadeIn"> <div class="asp_content"> <a class="asp_res_image_url" href="https://einsteam.com/contact/"> <div class="asp_image" style="background-image: url(&quot;http://einsteam.com/#contactus&quot;);"> <div class="void"></div> </div> </a> <h3><a class="asp_res_url" href="https://einsteam.com/contact/"> E-Commerce <span class="overlap"></span> </a></h3> <div class="etc"> </div> </div> <div class="clear"></div> </div> <div class="asp_spacer"></div><div class="item asp_result_pagepost asp_an_fadeIn"> <div class="asp_content"> <a class="asp_res_image_url" href="https://einsteam.com/contact/"> <div class="asp_image" style="background-image: url(&quot;http://einsteam.com/#contactus&quot;);"> <div class="void"></div> </div> </a> <h3><a class="asp_res_url" href="https://einsteam.com/contact/"> Retail <span class="overlap"></span> </a></h3> <div class="etc"> </div> </div> <div class="clear"></div> </div> <div class="asp_spacer"></div><div class="item asp_result_pagepost asp_an_fadeIn"> <div class="asp_content"> <a class="asp_res_image_url" href="https://einsteam.com/contact/"> <div class="asp_image" style="background-image: url(&quot;http://einsteam.com/#contactus&quot;);"> <div class="void"></div> </div> </a> <h3><a class="asp_res_url" href="https://einsteam.com/contact/"> Payment Industry <span class="overlap"></span> </a></h3> <div class="etc"> </div> </div> <div class="clear"></div> </div> <div class="asp_spacer"></div><div class="item asp_result_pagepost asp_an_fadeIn"> <div class="asp_content"> <a class="asp_res_image_url" href="https://einsteam.com/contact/"> <div class="asp_image" style="background-image: url(&quot;http://einsteam.com/#contactus&quot;);"> <div class="void"></div> </div> </a> <h3><a class="asp_res_url" href="https://einsteam.com/contact/"> Field Service <span class="overlap"></span> </a></h3> <div class="etc"> </div> </div> <div class="clear"></div> </div> <div class="asp_spacer"></div><div class="item asp_result_pagepost asp_an_fadeIn"> <div class="asp_content"> <a class="asp_res_image_url" href="https://einsteam.com/contact/"> <div class="asp_image" style="background-image: url(&quot;http://einsteam.com/#contactus&quot;);"> <div class="void"></div> </div> </a> <h3><a class="asp_res_url" href="https://einsteam.com/contact/"> SAS <span class="overlap"></span> </a></h3> <div class="etc"> </div> </div> <div class="clear"></div> </div> <div class="asp_spacer"></div></div> </div>所有列表以及zip contructor和DataFrame参数,按index选择行并获取loc值:

max

答案 1 :(得分:1)

选项1

您可以让Python的max完成工作并使用pandas.Series来保存结果

readings = [allReadings1, allReadings2, allReadings3, allReadings4, allReadings5,
            allReadings6, allReadings7, allReadings8, allReadings9, allReadings10]

s = pd.Series(dict(zip(allFeatures, map(max, zip(*readings)))))
s[aoiFeatures]

181    0.29
843    0.30
849    0.29
dtype: float64

选项2

或利用Numpy

readings = [allReadings1, allReadings2, allReadings3, allReadings4, allReadings5,
            allReadings6, allReadings7, allReadings8, allReadings9, allReadings10]

s = pd.Series(np.max(readings, 0), allFeatures)
s[aoiFeatures]

181    0.29
843    0.30
849    0.29
dtype: float64

如果您需要使用新读数更新最大值数组

allReadings11 =[0.13, 0.03, 0.30, 0.13, 0.30, 0.30, 0.28, 0.12, 0.19, 0.22]
s[:] = np.maximum(s, allReadings11)

s[aoiFeatures]

181    0.29
843    0.30
849    0.29
dtype: float64

答案 2 :(得分:0)

非常简单快捷的任务:

pd.DataFrame([allReadings1, allReadings2,...],columns=allFeatures).max()

示例输出:

enter image description here