gpt4 book ai didi

python - 如何在 DataFrame 列中匹配 'key off' 对

转载 作者:行者123 更新时间:2023-12-01 23:43:59 25 4
gpt4 key购买 nike

假设我有一个 df:

src    dest
LV NC
LA NY
NC LV
NY LA

如何“关闭”唯一的 src/dest 对,以便可以像这样在新列中引用它们?

src    dest     pair
LV NC 1
LA NY 2
NC LV 1
NY LA 2

df = pd.DataFrame({'src':['LV','LA','NC','NY'], 'dest':['NC', 'NY', 'LV', 'LA']})

最佳答案

尝试使用 tuple + set 然后 factorize

df['pair'] = df.apply(lambda x : tuple(set(x)),1).factorize()[0]+1
Out[69]: array([1, 2, 1, 2], dtype=int64)

关于python - 如何在 DataFrame 列中匹配 'key off' 对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64544598/

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