熊猫系列删除重复问题

时间:2016-05-30 08:47:29

标签: python pandas series

我有一个重复的系列,我试图摆脱

0     RWAY001
1     RWAY001
2     RWAY002
3     RWAY002
...
112    RWAY057
113    RWAY057
114    RWAY058
115    RWAY058
Length: 116

Drop.duplicates()似乎将长度减少到58,但索引似乎仍然从0到116,但只是跳过重复:

0      RWAY001
2      RWAY002
...
112    RWAY057
114    RWAY058
Length: 58

因此看起来中间的行仍然存在NaN值。我尝试过dropna()但它对数据没有任何影响。

这是我的代码:

  df = pd.read_csv(path + flnm)
  fields = df.file
  fields = fields.drop_duplicates()
  print fields

非常感谢任何帮助。感谢。

1 个答案:

答案 0 :(得分:1)

我认为您需要reset_index参数drop=True

fields.reset_index(inplace=True, drop=True)

或者:

fields = fields.reset_index(drop=True)

样品:

import pandas as pd

df = pd.DataFrame({'file': {0: 'RWAY001', 1: 'RWAY001', 2: 'RWAY002', 3: 'RWAY002', 115: 'RWAY058', 113: 'RWAY057', 112: 'RWAY057', 114: 'RWAY058'}})
print (df)
        file
0    RWAY001
1    RWAY001
2    RWAY002
3    RWAY002
112  RWAY057
113  RWAY057
114  RWAY058
115  RWAY058

print (df.file.drop_duplicates())
0      RWAY001
2      RWAY002
112    RWAY057
114    RWAY058
Name: file, dtype: object

print (df.file.drop_duplicates().reset_index(drop=True))
0    RWAY001
1    RWAY002
2    RWAY057
3    RWAY058
Name: file, dtype: object
相关问题