簡體   English   中英

我需要遍歷 pandas df 和 select 僅在第一列中具有特定值的特定行,然后 select 來自該行的值

[英]I need to iterate through a pandas df and select only specific rows with a specific value in the first column and then select a value from that row

我有一個 dataframe 看起來像這樣(它有更多的行和列,但這就是它的設置方式)

col1   col2     col3           col4  col5  col6  col7  col8
 MSH     a        b             e     e     r     a      d 
 PID     c   6002324^^^WAMT     d     s   PickB   x     
 OBR     e      pickC               PickD   v     z      q
 OBX     g        h             e           s     y       
 ORC     i        j             p     p     p     m      y
  \n   none      none         none  none  none  none   none
 MSH     a        b             e     e     r     a      d 
 PID     c    ^^^WAMT           d     s   PickF   x      o
 OBX     g        h             e     z     s     y      p 
 ORC     i        j                   p     p     m      y
 OBR     e      pickE               PickG   v     z      q
 OBX     g        h             e           s            t
 OBX     i        j             p     p     p     m      t
 OBX     g        h             e           s     y       
 OBX     i        j             p     p     p     m      y
  \n   none     none          none  none  none  none   none
 MSH     a        b             e     e     r     a      d 
 PID     c  43222346^^^WAMT     d     s   PickH   x      e
 OBX     g        h             e     z     s     y      p 
 ORC     i        j                   p     p     m      y
 OBR     e      pickI               PickJ   v     z      q
  \n   none      none         none  none  none  none   none
 MSH     a        b             e     e     r     a      d 
 PID     c    ^^^WAMT           d     s   PickK   x      o
 OBR     e      pickL               PickM   v     z      q
 OBX     g        h             e           s     y       

期望 output dataframe 看起來像這樣:

col1       col2     col3    col4
^^^WAMT    PickB    PickC   PickD
^^^WAMT    PickK    PickL   PickM

這是作為 DataFrame 構造函數的數據:

d = {'col1': ['MSH', 'PID', 'OBR', 'OBX', 'ORC', '/n', 'MSH', 'PID', 'OBX', 'ORC', 'OBR', 'OBX', 'OBX', 'OBX', 'OBX', '\n', 'MSH', 'PID', 'OBX', 'ORC', 'OBR', '\n', 'MSH', 'PID', 'OBR', 'OBX'], 'col2': ['a', 'b', 'e', 'g', 'i', 'none', 'a', 'c', 'g', 'i', 'e', 'g', 'i', 'g', 'i', 'none', 'a', 'c', 'g', 'i', 'e', 'none', 'a', 'c', 'e', 'g'], 'col3': ['b', '6002324^^^WAMT', 'pickC', 'h', 'j', 'nine', 'b', '^^^WAMT', 'h', 'j', 'PickE', 'h', 'j', 'h', 'j', 'none', 'b', '43222346^^^WAMT', 'h', 'j', 'PickI', 'none', 'b', '^^^WAMT', 'PickL', 'h'], 'col4': ['e', 'd', '', 'e', 'p', 'none', 'e', 'd', 'e', '', '', 'e', 'p', 'e', 'p', 'none', 'e', 'd', 'e', '', '', 'none', 'e', 'd', '', 'e'], 'col5': ['e', 's', 'PickD', '', 'p', 'none', 'e', 's', 'z', 'p', 'PickG', '', 'p', '', 'p', 'none', 'e', 's', 'z', 'p', 'PickJ', 'none', 'e', 's', 'PickM', ''], 'col6': ['r', 'PickB', 'v', 's', 'p', 'none', 'r', 'PickF', 's', 'p', 'v', 's', 'p', 's', 'p', 'none', 'r', 'PickH', 's', 'p', 'v', 'none', 'r', 'PickK', 'v', 's'], 'col7': ['a', 'b', 'e', '', 'i', 'none', 'a', 'c', 'g', 'i', '', 'g', 'i', 'g', 'i', 'none', 'a', 'c', 'g', 'i', 'e', 'none', 'a', 'c', 'e', 'g'], 'col8': ['a', 'b', 'e', 'g', 'i', 'none', 'a', 'c', 'g', 'i', 'e', 'g', 'i', '', 'i', 'none', 'a', 'c', 'g', 'i', 'e', 'none', 'a', 'c', 'e', '']}
df = pd.DataFrame(d)

我需要遍歷df中的每一行並檢查df的第一列是否等於PID,然后我需要檢查帶有^^^WMNT的字段是否在^^^WMNT之前有任何數字,或者它是否是空的,然后我想從 PID 中取出 ^^^WMNT 和 PickF,從 OBR 中取出 PickE 和 PickG,然后在新的 df 中取出它們。 但是,如果 PID 第 3 列在 ^^^WAMT 前面有一個數值,那么我不想將 PID 或 OBR 字段添加到新的 df 中。

所以我不知道是否更容易只提取所有 PID 和 OBR 行,然后遍歷它們以檢查 PickA 是否有值,或者是否可以一起完成。 我也不知道像它想要的那樣遍歷行和列的最佳方法。

到目前為止,我已嘗試使用此代碼遍歷 df,但我似乎沒有工作:

for row, index in range(len(df)):
   if df.loc[df[row] == 'MSH']:
      if df.loc[df[index] == 0]:
         # this is where i would have the pick this column value but I am not sure how to write this

任何幫助,將不勝感激。

您可以構建一個組以在 PID 上拆分,然后使用列表推導來提取數據並將其提供給 DataFrame 構造函數:

group = df['col1'].eq('PID').cumsum().values

out = pd.DataFrame([
    (g.loc['PID', 'col3'], g.loc['OBR', 'col3'], g.loc['PID', 'col6'], g.loc['OBR', 'col5'])
     for i,g in df.set_index('col1').groupby(group)
     if i and g.loc['PID', 'col3'] == '^^^WAMT'],
    columns=['A', 'B', 'C', 'D']
     )

print(out)

Output:

         A      B      C      D
0  ^^^WAMT  PickE  PickF  PickG
1  ^^^WAMT  PickL  PickK  PickM

暫無
暫無

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

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