![](/img/trans.png)
[英]Python Pandas: Denormalize data from one data frame into another
[英]Python/Pandas, Assign Data from one frame to Corresponding Data in Another
我在一幀(DF1)中有數據,其中包含群集的標簽,以及它們在latitude
和longitude
質心,還有第二個數據幀(DF2),其中包含一些地理位置社交媒體帖子的數據以及為其分配的群集的標簽使用DBSCAN。 我需要將DF1中的群集質心分配/映射到DF2中具有相應群集標簽的帖子。 使用熊貓,如何分配DF1的緯度和經度(質心)以匹配DF2中的標簽? 我嘗試使用.join()
和.merge()
進行這種連接,但是我收到各種類型和鍵錯誤。
樣本數據:
DF1
cluster_label latitude longitude frequency
0 39.18193382 -77.51885109 6
1 39.18 -77.27 46
2 39.17917928 -76.6688633 35
3 39.1782 -77.2617 48
4 39.1765 -77.1927 6
5 39.1762375 -76.8675441 16
6 39.17468 -76.8204499 7
7 39.17457332 -77.2807235 9
DF2
user_id timestamp latitude longitude cluster_label
3073171535 3/10/2017 11:10 39.18193382 -77.51885109 0
1628115950 3/11/2017 9:04 39.18193382 -77.51885109 0
7.46E+17 3/9/2017 21:52 39.18 -77.27 1
4188084947 3/10/2017 1:53 39.18 -77.27 1
3123690477 3/10/2017 10:44 39.17917928 -76.6688633 2
3063785591 3/10/2017 11:15 39.17917928 -76.6688633 2
2878413353 3/10/2017 14:33 39.1782 -77.2617 3
19410434 3/14/2017 15:32 39.1782 -77.2617 3
7.51E+17 3/9/2017 21:44 39.1765 -77.1927 4
27081288 3/14/2017 12:28 39.1765 -77.1927 4
400535528 3/12/2017 13:30 39.1762375 -76.8675441 5
8.06E+17 3/9/2017 19:45 39.1762375 -76.8675441 5
199324673 3/13/2017 11:12 39.17468 -76.8204499 6
198014146 3/13/2017 11:22 39.17468 -76.8204499 6
195546843 3/13/2017 11:26 39.17468 -76.8204499 6
2344467747 3/10/2017 21:45 39.1746 -77.2807 7
703776919 3/11/2017 20:14 39.1746 -77.2807 7
示例輸出
user_id timestamp cluster_label cluster_centroid_lat cluster_centroid_long
3073171535 3/10/2017 11:10 0 39.18193382 -77.51885109
7.46E+17 3/9/2017 21:52 1 39.18 -77.27
merged=df2.merge(df1,on='cluster_label')
並刪除您想要的任何列...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.