gpt4 book ai didi

python - Pandas - 添加一列以在 group-by 后唯一标识每个组

转载 作者:行者123 更新时间:2023-11-28 16:55:56 24 4
gpt4 key购买 nike

我想在使用 s_lat, s_lng, d_lat, d_lng 列进行分组后针对每个组创建一个列 (Similarity_Index),以便唯一地标识它们。我的数据框看起来像这样:


+--------+-------+--------+--------+--------+------------------+------+-------+------+--------------------+-------+---------+
| Id | s_lat | s_lng | d_lat | d_lng | TT | T | Esti2 | Est1 | time_diff | diff | Cluster |
+--------+-------+--------+--------+--------+------------------+------+-------+------+--------------------+-------+---------+
| 67607 | 63.42 | 10.387 | 63.425 | 10.441 | 10.2 | 4.33 | 11 | 4.4 | -0.800000000000001 | -0.07 | 0 |
| 70720 | 63.42 | 10.387 | 63.425 | 10.441 | 9.03333333333333 | 4.36 | 11 | 4.4 | -1.96666666666667 | -0.04 | 0 |
| 68394 | 63.42 | 10.387 | 63.436 | 10.399 | 15.1833333333333 | 2.66 | 10 | 2.7 | 5.18333333333333 | -0.04 | 0 |
| 67340 | 63.42 | 10.387 | 63.436 | 10.399 | 8.91666666666667 | 2.44 | 10 | 2.7 | -1.08333333333333 | -0.26 | 0 |
| 72363 | 63.42 | 10.387 | 63.436 | 10.399 | 9.91666666666667 | 2.47 | 10 | 2.7 | -0.083333333333334 | -0.23 | 0 |
| 70401 | 63.42 | 10.387 | 63.436 | 10.399 | 7.85 | 2.67 | 10 | 2.7 | -2.15 | -0.03 | 0 |
| 70695 | 63.42 | 10.387 | 63.436 | 10.399 | 11.6166666666667 | 3.24 | 10 | 2.7 | 1.61666666666667 | 0.54 | 0 |
| 69698 | 63.42 | 10.387 | 63.436 | 10.399 | 8.91666666666667 | 2.47 | 10 | 2.7 | -1.08333333333333 | -0.23 | 0 |
| 70793 | 63.42 | 10.387 | 63.436 | 10.399 | 11.85 | 2.52 | 10 | 2.7 | 1.85 | -0.18 | 0 |
| 67150 | 63.42 | 10.387 | 63.411 | 10.402 | 4.01666666666667 | 1.68 | 6 | 1.7 | -1.98333333333333 | -0.02 | 0 |
| 69934 | 63.42 | 10.387 | 63.411 | 10.402 | 4.56666666666667 | 1.69 | 6 | 1.7 | -1.43333333333333 | -0.01 | 0 |
+--------+-------+--------+--------+--------+------------------+------+-------+------+--------------------+-------+---------+

s_lat | s_lng | d_lat | d_lng 定义起点和终点的纬度和经度,我正在尝试根据位置对相似的行程进行配对,实现它的最佳方法是什么。

期望的输出看起来像这样

+--------+-------+--------+--------+--------+------------------+------+-------+------+--------------------+-------+---------+------------------+
| TourId | s_lat | s_lng | d_lat | d_lng | TT | T | Esti2 | Est1 | time_diff | diff | Cluster | Similarity_index |
+--------+-------+--------+--------+--------+------------------+------+-------+------+--------------------+-------+---------+------------------+
| 67607 | 63.42 | 10.387 | 63.425 | 10.441 | 10.2 | 4.33 | 11 | 4.4 | -0.800000000000001 | -0.07 | 0 | A |
| 70720 | 63.42 | 10.387 | 63.425 | 10.441 | 9.03333333333333 | 4.36 | 11 | 4.4 | -1.96666666666667 | -0.04 | 0 | A |
| 68394 | 63.42 | 10.387 | 63.436 | 10.399 | 15.1833333333333 | 2.66 | 10 | 2.7 | 5.18333333333333 | -0.04 | 0 | B |
| 67340 | 63.42 | 10.387 | 63.436 | 10.399 | 8.91666666666667 | 2.44 | 10 | 2.7 | -1.08333333333333 | -0.26 | 0 | B |
| 72363 | 63.42 | 10.387 | 63.436 | 10.399 | 9.91666666666667 | 2.47 | 10 | 2.7 | -0.083333333333334 | -0.23 | 0 | B |
| 70401 | 63.42 | 10.387 | 63.436 | 10.399 | 7.85 | 2.67 | 10 | 2.7 | -2.15 | -0.03 | 0 | B |
| 70695 | 63.42 | 10.387 | 63.436 | 10.399 | 11.6166666666667 | 3.24 | 10 | 2.7 | 1.61666666666667 | 0.54 | 0 | B |
| 69698 | 63.42 | 10.387 | 63.436 | 10.399 | 8.91666666666667 | 2.47 | 10 | 2.7 | -1.08333333333333 | -0.23 | 0 | B |
| 70793 | 63.42 | 10.387 | 63.436 | 10.399 | 11.85 | 2.52 | 10 | 2.7 | 1.85 | -0.18 | 0 | B |
| 67150 | 63.42 | 10.387 | 63.411 | 10.402 | 4.01666666666667 | 1.68 | 6 | 1.7 | -1.98333333333333 | -0.02 | 0 | C |
| 69934 | 63.42 | 10.387 | 63.411 | 10.402 | 4.56666666666667 | 1.69 | 6 | 1.7 | -1.43333333333333 | -0.01 | 0 | C |
+--------+-------+--------+--------+--------+------------------+------+-------+------+--------------------+-------+---------+------------------+

最佳答案

你需要这个

import string
d = dict(enumerate(list(string.ascii_uppercase))) # will give 26 unique values
df['S_I']=df.groupby(['s_lat','s_lng','d_lat','d_lng'], sort = False).ngroup().map(d)

如果可以有超过 26 个唯一组,则可以使用以下代码生成列表 d

from itertools import product
combs= [''.join(i) for i in product(string.ascii_uppercase, repeat = 2)]
d=dict(enumerate(combs))

#Change the number for 'repeat' as needed.
with 2, you get 676 unique combinations like "'AA','AB','AC'..."
with 3, you get 17576 unique combinations like "'AAA','AAB','AAC'..."

输出

Id  s_lat   s_lng   d_lat   d_lng   TT  T   Esti2   Est1    time_diff   diff    Cluster     S_I
0 67607 63.42 10.387 63.425 10.441 10.200000 4.33 11 4.4 -0.800000 -0.07 0 A
1 70720 63.42 10.387 63.425 10.441 9.033333 4.36 11 4.4 -1.966667 -0.04 0 A
2 68394 63.42 10.387 63.436 10.399 15.183333 2.66 10 2.7 5.183333 -0.04 0 B
3 67340 63.42 10.387 63.436 10.399 8.916667 2.44 10 2.7 -1.083333 -0.26 0 B
4 72363 63.42 10.387 63.436 10.399 9.916667 2.47 10 2.7 -0.083333 -0.23 0 B
5 70401 63.42 10.387 63.436 10.399 7.850000 2.67 10 2.7 -2.150000 -0.03 0 B
6 70695 63.42 10.387 63.436 10.399 11.616667 3.24 10 2.7 1.616667 0.54 0 B
7 69698 63.42 10.387 63.436 10.399 8.916667 2.47 10 2.7 -1.083333 -0.23 0 B
8 70793 63.42 10.387 63.436 10.399 11.850000 2.52 10 2.7 1.850000 -0.18 0 B
9 67150 63.42 10.387 63.411 10.402 4.016667 1.68 6 1.7 -1.983333 -0.02 0 C
10 69934 63.42 10.387 63.411 10.402 4.566667 1.69 6 1.7 -1.433333 -0.01 0 C

关于python - Pandas - 添加一列以在 group-by 后唯一标识每个组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58322646/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com