如何检测 pandas 上的特征重复
How detect feature duplication on pandas
这是我的数据
Id feature1 feature2 feature3 feature4 feature5 feature6
1 4 5 7 7 4 5
2 5 6 8 8 5 5
我要的是去掉重复数据
Id feature1 feature2 feature3 feature6
1 4 5 7 5
2 5 6 8 5
如果也描述重复就更好了
feature3 is same with feature4
feature2 is same with feature5
通常,我使用 seaboarn corplot,但当特征增长超过 100 时,我会感到困惑
import seaborn as sns
ax = sns.heatmap(df)
您可以使用 df.T
to transpose your dataframe, use drop_duplicates
,然后再次转置数据帧:
In [6]: df.T.drop_duplicates().T
Out[6]:
Id feature1 feature2 feature3 feature6
0 1 4 5 7 5
1 2 5 6 8 5
您可以使用 T
然后 groupby
值,请注意 drop_duplicates
和 duplicated
不会提供对,这意味着它们只会返回重复的值(不重复组)
s=df.T.reset_index().groupby([0,1])['index'].apply(tuple)
s[s.str.len()>=2].apply(lambda x : '{0[0]} is same with {0[1]}'.format(x))
Out[797]:
0 1
4 5 feature1 is same with feature5
7 8 feature3 is same with feature4
Name: index, dtype: object
可能的解决方案是 drop_duplicates() 方法。但是,它会查找行,因此您应该将其应用于转置数据框,然后再次转置结果。示例:
data = [
[4, 5, 7, 7, 4, 5],
[5, 6, 8, 8, 5, 5],
]
columns=['feature1', 'feature2', 'feature3', 'feature4', 'feature5', 'feature6']
df = pd.DataFrame(data, columns)
df.T.drop_duplicates().T
为了显示哪些特征是重复的,可以使用duplicated()方法
df.T.duplicated().T
将显示:
feature1 False
feature2 False
feature3 False
feature4 True
feature5 True
feature6 False
dtype: bool
这是我的数据
Id feature1 feature2 feature3 feature4 feature5 feature6
1 4 5 7 7 4 5
2 5 6 8 8 5 5
我要的是去掉重复数据
Id feature1 feature2 feature3 feature6
1 4 5 7 5
2 5 6 8 5
如果也描述重复就更好了
feature3 is same with feature4
feature2 is same with feature5
通常,我使用 seaboarn corplot,但当特征增长超过 100 时,我会感到困惑
import seaborn as sns
ax = sns.heatmap(df)
您可以使用 df.T
to transpose your dataframe, use drop_duplicates
,然后再次转置数据帧:
In [6]: df.T.drop_duplicates().T
Out[6]:
Id feature1 feature2 feature3 feature6
0 1 4 5 7 5
1 2 5 6 8 5
您可以使用 T
然后 groupby
值,请注意 drop_duplicates
和 duplicated
不会提供对,这意味着它们只会返回重复的值(不重复组)
s=df.T.reset_index().groupby([0,1])['index'].apply(tuple)
s[s.str.len()>=2].apply(lambda x : '{0[0]} is same with {0[1]}'.format(x))
Out[797]:
0 1
4 5 feature1 is same with feature5
7 8 feature3 is same with feature4
Name: index, dtype: object
可能的解决方案是 drop_duplicates() 方法。但是,它会查找行,因此您应该将其应用于转置数据框,然后再次转置结果。示例:
data = [
[4, 5, 7, 7, 4, 5],
[5, 6, 8, 8, 5, 5],
]
columns=['feature1', 'feature2', 'feature3', 'feature4', 'feature5', 'feature6']
df = pd.DataFrame(data, columns)
df.T.drop_duplicates().T
为了显示哪些特征是重复的,可以使用duplicated()方法
df.T.duplicated().T
将显示:
feature1 False
feature2 False
feature3 False
feature4 True
feature5 True
feature6 False
dtype: bool