簡體   English   中英

與熊貓python內部連接

[英]inner join with group by pandas python

我有兩個名為geostat和ref的數據框,這些數據框如下:

geostat:
      count percent  grpno. state code
0          14.78       1         CA
1           0.00       2         CA
2           8.80       3         CA
3           9.60       4         FL
4          55.90       4         MA
5           0.00       2         FL
6           0.00       6         NC
7           0.00       5         NC
8           6.90       1         FL
9          59.00       4         MA
res:
    grpno.  MaxOfcount percent
0       1               14.78
1       2                0.00
2       3                8.80
3       4               59.00
4       5                0.00
5       6                0.00

我想從數據框geostat中選擇first(res.Maxofcount百分比),res.grpno。和geostat.first(狀態碼),並在res.Maxofcount百分比= geostat.count百分比AND res列上進行res內部聯接。 grpno。 = geostat.grpno。 按res.grpno分組。

我想做這個python pandas,我不確定如何使用group by進行內部聯接,有人可以幫我嗎?

輸出數據幀如下:

   FirstOfMaxOfState count percent  state pool number FirstOfstate code
0                            14.78                  1                CA
1                             0.00                  2                CA
2                             8.80                  3                CA
3                            59.00                  4                MA
4                             0.00                  5                NC
5                             0.00                  6                NC

注意:FIRST(Column name)是一個訪問函數,在python中應該等效於什么?

編輯:更改了輸出數據框。

使用pandas.DataFrame.merge()

geostat.merge(res, left_on=['count percent', 'grpno.'], right_on=['MaxOfcount percent', 'grpno.'],how='inner')

   count percent  grpno. state code  MaxOfcount percent
0          14.78       1         CA               14.78
1           0.00       2         CA                0.00
2           0.00       2         FL                0.00
3           8.80       3         CA                8.80
4           0.00       6         NC                0.00
5           0.00       5         NC                0.00
6          59.00       4         MA               59.00

暫無
暫無

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

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