Pandas系列消除重复问题

2024-05-26 21:52:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一系列的重复,我正在努力摆脱

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

在删除重复项()似乎将长度缩短到58,但索引似乎仍从0变为116,但只是跳过重复项:

^{pr2}$

因此,似乎中间的行仍然以NaN值存在。我尝试了dropna(),但它对数据没有任何影响。在

这是我的代码:

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

谢谢你的帮助。谢谢。在


Tags: csv数据代码fieldsdfreadnanlength
1条回答
网友
1楼 · 发布于 2024-05-26 21:52:24

我想您需要^{}和参数drop=True

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

或者:

^{pr2}$

样品:

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

相关问题 更多 >

    热门问题