簡體   English   中英

根據來自另一個數據框的數據將值分配給Pandas數據框中的列

[英]Assign values to columns in Pandas Dataframe based on data from another dataframe

我有兩個包含濃度數據和坐標的數據框:

濃度數據(濃度):

    Sample  analParam                 Conc  Units
0   CW7-1   1,1,1-Trichloroethane     0     UG/L
1   CW7-1   1,1,2,2-Tetrachloroethane 0     UG/L
2   CW7-1   1,1,2-Trichloroethane     0     UG/L
3   CW7-1   1,1-Dichloroethane        0     UG/L
4   CW7-1   1,1-Dichloroethylene      0     UG/L
5   CW7-1   1,1-Dichloropropene       0     UG/L
6   CW7-1   1,2,3-Trichlorobenzene    0     UG/L
... ... ... ... ...
50311   VOA2-2  Tetrachloroethylene  1.8    MG/KG
50312   VOA2-2  Toluene              1.2    MG/KG
50313   VOA2-2  Trichloroethylene    1.8    MG/KG
50314   VOA2-2  Vinyl Chloride       1.8    MG/KG

坐標數據(坐標):

    Sample  x            y
0   CW7-1   320800.000  396500.000
1   CW7-2   320800.000  396500.000
2   CW7-3   320800.000  396500.000
3   FB06-17 0.000       0.000
4   FB06-18 0.000       0.000
5   FB06-19 0.000       0.000
6   FB07-08 0.000       0.000
... ... ... ...
453 TP21-1  318807.281  398547.485
454 TP21-2  318807.281  398547.485
455 TP24-1  318489.248  398544.797
456 VOA1-1  318500.582  398573.558
457 VOA1-2  318500.582  398573.558
458 VOA2-1  318536.337  398589.805
459 VOA2-2  318536.337  398589.805

我想在濃度數據框中添加兩列,其中包含每種濃度的相應樣品ID的所有坐標。 例如,濃度數據的前六行將具有x = 320800和y = 396500的列,因為它們的樣本ID均為CW7-1:

    Sample  analParam                 Conc  Units   x       y
0   CW7-1   1,1,1-Trichloroethane     0     UG/L    320800  396500   
1   CW7-1   1,1,2,2-Tetrachloroethane 0     UG/L    320800  396500
2   CW7-1   1,1,2-Trichloroethane     0     UG/L    320800  396500  
3   CW7-1   1,1-Dichloroethane        0     UG/L    320800  396500  
4   CW7-1   1,1-Dichloroethylene      0     UG/L    320800  396500  
5   CW7-1   1,1-Dichloropropene       0     UG/L    320800  396500

我已經嘗試過使用double for循環,但是由於我有很多數據點,它的速度太慢了:

for index, row in conc.iterrows():
    for cindex, crow in coord.iterrows():
        if conc.iloc[index,0] == coord.iloc[cindex,0]:
            conc.at[index,4] = coord.iloc[cindex,1]
            conc.at[index,5] = coord.iloc[cindex,2]

我已經嘗試過使用apply函數,但是卻不斷出錯。 對於此演示,我得到了TypeError: call ()從1到2個位置參數,但是給出了3個。

def xcoord (i):
    for index, row in coord.iterrows():
        if i == coord.iloc[index,0] :
            return coord.iloc(index,4)
conc['Sample'].apply(xcoord)

謝謝溫!

In[1]:
conc.merge(coord,on='Sample',how='left')

Out[1]:
Sample  analParam   Conc    Units   x   y
0   CW7-1   1,1,1-Trichloroethane   0   UG/L    320800.000  396500.000
1   CW7-1   1,1,2,2-Tetrachloroethane   0   UG/L    320800.000  396500.000
2   CW7-1   1,1,2-Trichloroethane   0   UG/L    320800.000  396500.000
3   CW7-1   1,1-Dichloroethane  0   UG/L    320800.000  396500.000
4   CW7-1   1,1-Dichloroethylene    0   UG/L    320800.000  396500.000
5   CW7-1   1,1-Dichloropropene 0   UG/L    320800.000  396500.000
6   CW7-1   1,2,3-Trichlorobenzene  0   UG/L    320800.000  396500.000
... ... ... ... ... ... ...
50311   VOA2-2  Tetrachloroethylene 1.8 MG/KG   318536.337  398589.805
50312   VOA2-2  Toluene 1.2 MG/KG   318536.337  398589.805
50313   VOA2-2  trans-1,3-Dichloropropene   1.8 MG/KG   318536.337  398589.805
50314   VOA2-2  Trichloroethylene   1.8 MG/KG   318536.337  398589.805
50315   VOA2-2  Vinyl Chloride  1.8 MG/KG   318536.337  398589.805
50316   VOA2-2  Xylenes (Total) 2.6 MG/KG   318536.337  398589.805

暫無
暫無

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

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