簡體   English   中英

熊貓:最后一個非相等行的索引

[英]Pandas: Index of last non equal row

我有一個帶有排序索引I的熊貓數據框F 我有興趣知道其中一列的最新變化,比如說A 特別是,我想構造一個具有與F相同的索引的序列,即I ,其在i值為j ,其中j是小於i的最大索引值,從而F[A][j] != F[A][i] 例如,考慮以下框架:

  A
1 5
2 5
3 6
4 2
5 2

所需的序列為:

1 NaN
2 NaN
3   2
4   3
5   3

有沒有熊貓/ numpy慣用的方式來構造這個系列?

嘗試這個:

df['B'] = np.nan
last = np.nan
for index, row in df.iterrows():
    if index == 0:
        continue
    if df['A'].iloc[index] != df['A'].iloc[index - 1]:
        last = index
    df['B'].iloc[index] = last

這將使用結果創建一個新列。 我認為在行中更改行並不是一個好主意,在那之后,您可以簡單地替換一列並刪除另一行。

布爾數據上的np.argmaxpd.Series.argmax可以幫助您找到第一個(在本例中為最后一個) True值。 不過,您仍然必須在此解決方案中循環討論該系列。

# Initiate source data
F = pd.DataFrame({'A':[5,5,6,2,2]}, index=list('fobni'))

# Initiate resulting Series to NaN
result = pd.Series(np.nan, index=F.index)

for i in range(1, len(F)):
    value_at_i = F['A'].iloc[i]
    values_before_i = F['A'].iloc[:i]
    # Get differences as a Boolean Series
    # (keeping the original index)
    diffs = (values_before_i != value_at_i)
    if diffs.sum() == 0:
        continue
    # Reverse the Series of differences,
    # then find the index of the first True value
    j = diffs[::-1].argmax()
    result.iloc[i] = j

暫無
暫無

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

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