我有一个表单
的DataFrameperson1, person2, ..., someMetric
John, Steve, ..., 20
Peter, Larry, ..., 12
Steve, John, ..., 20
第0行和第2行是可互换的重复项,所以我想删除最后一行。我无法弄清楚如何在熊猫中做到这一点。
谢谢!
答案 0 :(得分:2)
这是基于NumPy的解决方案 -
df[~(np.triu(df.person1.values[:,None] == df.person2.values)).any(0)]
示例运行 -
In [123]: df
Out[123]:
person1 person2 someMetric
0 John Steve 20
1 Peter Larry 13
2 Steve John 19
3 Peter Parker 5
4 Larry Peter 7
In [124]: df[~(np.triu(df.person1.values[:,None] == df.person2.values)).any(0)]
Out[124]:
person1 person2 someMetric
0 John Steve 20
1 Peter Larry 13
3 Peter Parker 5
答案 1 :(得分:1)
pandas中的一种方法
df = pd.DataFrame(
{'person2': {0: 'Steve', 1: 'Larry', 2: 'John', 3: 'Parker', 4: 'Peter'},
'person1': {0: 'John', 1: 'Peter', 2: 'Steve', 3: 'Peter', 4: 'Larry'},
'someMetric': {0: 20, 1: 13, 2: 19, 3: 5, 4: 7}})
print(df)
person1 person2 someMetric
0 John Steve 20
1 Peter Larry 13
2 Steve John 19
3 Peter Parker 5
4 Larry Peter 7
df['ordered-name'] = df.apply(lambda x: '-'.join(sorted([x['person1'],x['person2']])),axis=1)
df = df.drop_duplicates(['ordered-name'])
df.drop(['ordered-name'], axis=1, inplace=True)
print df
给出:
person1 person2 someMetric
0 John Steve 20
1 Peter Larry 13
3 Peter Parker 5