簡體   English   中英

使用 Pandas 找出兩組數據之間的差異

[英]Finding the difference between two sets of data using Pandas

我有兩個數組,每個數組由數百個不同的值組成,可以說,

list1 = [23.13, 89.50, 12.99, 40.89,...]

list2 = [45.21, 2.02, 79.89, 20.30, ....]

我有一個單獨的數組,它指示我想從每個數組中比較的條目的索引(0 表示兩個數組中的第一個條目,434 表示第 435 個條目,依此類推)。 讓我們調用這個數組

索引 = [0, 12, 304, ...]

我的目標是從索引在 indx 中列出的兩個列表中獲取條目,減去這些值,然后將它們存儲在另一個數組中。 我想使用 Pandas,因為我的大部分代碼都使用了 Pandas。 謝謝你。

如果你有

list1 = np.array([23.13, 89.50, 12.99, 40.89])
list2 = np.array([45.21, 2.02 , 79.89, 20.30])

indx = [0, 2, 3]

然后簡單地

>>> list1[indx] - list2[indx]
array([-22.08, -66.9 ,  20.59])

同樣,如果你有pd.Series而不是np.array

pd.Series(list1)[indx] - pd.Series(list2)[indx]

0   -22.08
2   -66.90
3    20.59
dtype: float64

對於不對稱長度,

nindx = indx[indx < min(map(len, [list1, list2]))]
>>> list1[nindx] - list2[nindx]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM