![](/img/trans.png)
[英]Populate one dataframe based on information in another dataframe
[英]Editting values in a dataframe based of the information in another dataframe
我有一個名為_df1的數據框,它看起來像這樣。 請注意,這不是整個數據框,而是其中的一部分。
_df1:
frame id x1 y1 x2 y2
1 1 1363 569 103 241
2 1 1362 568 103 241
3 1 1362 568 103 241
4 1 1362 568 103 241
964 5 925 932 80 255
965 5 925 932 79 255
966 5 925 932 79 255
967 5 924 932 80 255
968 5 924 932 79 255
16 6 631 761 100 251
17 6 631 761 100 251
18 6 631 761 100 251
19 6 631 761 100 251
20 6 631 761 100 251
21 6 631 761 100 251
88 7 623 901 144 123
89 7 623 901 144 123
90 7 623 901 144 123
91 7 623 901 144 123
92 7 623 901 144 123
93 7 623 901 144 123
94 7 623 901 144 123
在完整的數據庫中,數據框中有108003行和141個唯一ID。 ID代表特定對象,只要該幀具有該對象,就重復該ID。 換句話說,我的數據有141個不同的對象和1080.03幀。 我寫了一段代碼來識別具有相同對象但標有不同ID的框架。 這被保存在另一個名為_df2的數據框中,如下所示。 這也只是數據幀的一部分,而不是全部。
_df2:
indexID matchID
4 5
6 7
8 9
12 13
18 19
20 21
.
.
.
第二個數據幀顯示哪些索引被錯誤地分類為另一個對象。 這意味着'matchID'中的ID實際上是與'indexID'相同的對象。 _df2中的“ indexID”對應於_df1中的“ id”。
以_df2中的第一行為例,它說索引4和5相同。 因此,我需要將所有具有'id'5的幀的_df1中的'id'值更改為4。這是最終表的外觀示例,因為必須將5歸類為4,而將7歸類為歸類為6。
輸出:
frame id x1 y1 x2 y2
1 1 1363 569 103 241
2 1 1362 568 103 241
3 1 1362 568 103 241
4 1 1362 568 103 241
964 4 925 932 80 255
965 4 925 932 79 255
966 4 925 932 79 255
967 4 924 932 80 255
968 4 924 932 79 255
16 6 631 761 100 251
17 6 631 761 100 251
18 6 631 761 100 251
19 6 631 761 100 251
20 6 631 761 100 251
21 6 631 761 100 251
88 6 623 901 144 123
89 6 623 901 144 123
90 6 623 901 144 123
91 6 623 901 144 123
92 6 623 901 144 123
93 6 623 901 144 123
94 6 623 901 144 123
使用replace
df1.id=df.id.replace(dict(zip(df2.indexID,df2.matchID)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.