如何使用pandas python获取系列的重复值及其索引?

时间:2017-11-23 06:29:55

标签: python pandas

import pandas as pd

Fzytion_val = [13055, 13563, 13867, 14696, 15460, 15311, 15311, 15603, 15603, 15861, 16807, 16807, 16919, 16388, 16388, 15433, 15497, 15145, 15163, 15163, 15984, 15984, 16859, 16859, 18150, 18970, 18970, 19328, 19337, 18876]
vc = pd.Series(Fzytion_val)
df =vc[vc.duplicated(keep=False)].tolist()

我得到重复的值

 [15311, 15311, 15603, 15603, 16807, 16807, 16388, 16388, 15163, 15163, 15984, 15984, 16859, 16859, 18970, 18970]

但我也想要这些值的索引并将它们保存在另一个列表中

[5, 6, 7, 8, 10, 11, 13, 14, 18, 19, 20, 21, 22, 23, 25, 26]

duplicated函数只返回一个布尔值数组,那么pandas中有另一种获取索引的方法吗?

提前致谢

1 个答案:

答案 0 :(得分:3)

首先为已过滤的s创建变量Series,然后将索引和值转换为列表:

s = vc[vc.duplicated(keep=False)]
L1 = s.index.tolist()
L2 = s.tolist()

替代方案(应该更快)首先转换为numpy array然后转换为list s:

L1 = s.index.values.tolist()
L2 = s.values.tolist()
print (L1)
[5, 6, 7, 8, 10, 11, 13, 14, 18, 19, 20, 21, 22, 23, 25, 26]
print (L2)
[15311, 15311, 15603, 15603, 16807, 16807, 16388, 16388, 15163, 
 15163, 15984, 15984, 16859, 16859, 18970, 18970]