我在数据帧中有一个无向连接网络。
Source_ID Target_ID
0 1 5
1 7 2
2 12 6
3 3 9
4 16 11
5 2 7 <------The same as row 1
6 4 8
7 5 1 <------The same as row 0
8 99 81
但由于这是一个无向网络,所以第0行和第7行在技术上是相同的,就像第1行和第5行一样。df.drop_duplicates()
不够聪明,不知道如何将它们作为重复项消除,因为它将它们视为两个不同的行,至少就我所做的尝试而言是这样。
我还尝试了我认为应该有效的方法,即使用Source_ID
和Target_ID
的索引,并将Source_ID
设置为比target_ID
“更低”。但这似乎也没有产生我需要的结果。
df.drop(df.loc[df['Target_ID'] < d['Source_ID']]
.index.tolist(), inplace=True)
因此,我需要找出一种方法来删除重复的连接(同时保留第一个连接),以便我的固定数据帧看起来像(在索引重置之后):
Source_ID Target_ID
0 1 5
1 7 2
2 12 6
3 3 9
4 16 11
5 4 8
6 99 81
发布于 2019-11-28 12:17:55
当然不是最有效的,但可能会完成这项工作:
df.apply(lambda row: pd.Series() if row[::-1].values in df.values \
and row[0] < row[1] else row, axis=1).dropna().reset_index(drop=True)
https://stackoverflow.com/questions/59081386
复制相似问题