简体   繁体   English

将唯一 ID 分配给 Pandas 数据框中两列的组合,按其顺序独立

[英]Assign unique ID to combination of two columns in pandas dataframe independently on their order

I have a dataframe like this我有一个这样的数据框

col1 col2
1    2
2    1
2    3
3    2
3    4
4    3

and I would like to assign to each row a unique dataset based on col1 and col2 but independently on their order我想为每一行分配一个基于 col1 和 col2 但独立于它们的顺序的唯一数据集

col1 col2 id
1    2    1
2    1    1
2    3    2
3    2    2
3    4    3
4    3    3

How can I do this?我怎样才能做到这一点?

One approach:一种方法:

df["id"] = df.groupby(df[["col1", "col2"]].apply(frozenset, axis=1)).ngroup() + 1
print(df)

Output输出

   col1  col2  id
0     1     2   1
1     2     1   1
2     2     3   2
3     3     2   2
4     3     4   3
5     4     3   3

Alternative using np.unique + np.sort :使用np.unique + np.sort替代方法:

_, indices = np.unique(np.sort(df.values, axis=1), return_inverse=True, axis=0)
df["id"] = indices + 1
print(df)

Output输出

   col1  col2  id
0     1     2   1
1     2     1   1
2     2     3   2
3     3     2   2
4     3     4   3
5     4     3   3

You can apply it:你可以apply它:

import pandas as pd

df = pd.DataFrame(data={"col1":[1,2,3,1,2,3], "col2":[3,2,1,3,2,1]})
df['id'] = df.apply(lambda row: min(row.col1, row.col2), axis=1)
print(df)

output:输出:

   col1  col2  id
0     1     3   1
1     2     2   2
2     3     1   1
3     1     3   1
4     2     2   2
5     3     1   1

Try np.sort :试试np.sort

a = np.sort(df, axis=1)
df['id'] = df.groupby([a[:,0],a[:,1]]).ngroup() + 1

Output:输出:

   col1  col2  id
0     1     2   1
1     2     1   1
2     2     3   2
3     3     2   2
4     3     4   3
5     4     3   3

Can also use:还可以使用:

df['mask'] = df.apply(lambda x:','.join(map(str, x.sort_values())), axis=1)
df['id'] = (df['mask'] != df['mask'].shift()).cumsum()
df.drop(columns=['mask'], inplace=True)

Output:输出:

   col1  col2  id
0     1     2   1
1     2     1   1
2     2     3   2
3     3     2   2
4     3     4   3
5     4     3   3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM