如果在下面的示例中找到一个非唯一值,我将尝试删除所有行:
class AStudentType[K, V](
var name: String ="Jack",
val map: WeakHashMap[K, V] = new WeakHashMap()
)
val AStudent = new AStudentType[String, String]()
因此,在这种情况下,我想要的值是2 5 7和14。而且一列比另一列长,因此必须忽略NaN。我基本上想找到重复的值并从N1和N2中删除。这是我尝试过的:
N1 N2
1 2 4
2 4 5
3 6 6
4 8 7
5 10 8
6 12 10
7 NaN 12
8 NaN 14
出现了一些错误。谢谢您的帮助。
凯文
答案 0 :(得分:1)
快速解决方案:
>> df.stack().drop_duplicates(keep=False).unstack()
N1 N2
1 2.0 NaN
2 NaN 5.0
4 NaN 7.0
8 NaN 14.0
作为列表:
>> df.stack().drop_duplicates(keep=False).values.tolist()
[2.0, 5.0, 7.0, 14.0]
答案 1 :(得分:0)
这是可以实现的方式:
from io import StringIO
import pandas as pd
s = '''N1 N2
2 4
4 5
6 6
8 7
10 8
12 10
NaN 12
NaN 14'''
ss = StringIO(s)
df = pd.read_csv(ss, sep=r'\s+')
df = df.dropna()
df[~df.N1.isin(['N2'])]
答案 2 :(得分:0)
根据您发布的值创建一个数据框:
import numpy as np
import pandas as pd
df = pd.DataFrame({'N1':[2, 4, 6, 8, 10, 12, np.nan, np.nan],
'N2':[4,5,6,7,8,10,12,14]})
找到常用值:
common = list(set(df['N1']) & set(df['N2']))
排除所有N1
或N2
具有其中之一的行:
df[(~df["N1"].isin(common)) | (~df["N2"].isin(common))]
更新
common = set(df['N1']) & set(df['N2'])
result = list(set(df['N2'])-common) + list(set(df['N1'])-common)
result = [x for x in result if x==x]