我有以下数据框-
print df
Name | Role |
Mark | Admin |
Mark | Admin. |
df = df.drop_duplicates()
print df
Name | Role |
Mark | Admin |
Mark | Admin. |
我想忽略任何前导或前置标点符号(在这种情况下为句号)并删除重复项。
预期产量-
df = df.drop_duplicates()
print df
Name | Role |
Mark | Admin |
使用Series.str.strip
所有punctuations
与空间与所有列DataFrame.apply
,让所有重复DataFrame.duplicated
的和筛选boolean indexing
:
import string
df = df[~df.apply(lambda x: x.str.strip(string.punctuation + ' ')).duplicated()]
print (df)
Name Role
0 Mark Admin
另一个想法是处理已删除的数据punctuation
:
import string
df1 = df.apply(lambda x: x.str.strip(string.punctuation + ' ')).drop_duplicates()
print (df1)
Name Role
0 Mark Admin
详情
#added list for see last space
print ([string.punctuation + ' '])
['!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ ']
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句