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]
复制的函数仅返回布尔值数组,因此pandas中还有另一种获取索引的方法吗?
提前致谢
最佳答案
首先为过滤的s
创建变量Series
,然后将索引和值转换为列表:
s = vc[vc.duplicated(keep=False)]
L1 = s.index.tolist()
L2 = s.tolist()
替代方法(应该更快)首先转换为
numpy array
,然后转换为list
: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]