我在数据框中有一个无向的连接网络。
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
答案 0 :(得分:0)
肯定不是最有效的,但是可以完成工作:
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)