'Unique index in pandas
I have a dataframe similar to
A B C
1 a b
1 b c
1 e f
2 d g
2 x y
But I want a dataframe with values in Column A occuring only once like below
A
B C
1 a b
b c
e f
2 d g
x y
I tried to use setting column A as index and also tried groupby. But the dataframe looked same as before.
Solution 1:[1]
You can replace duplicated values to empty strings, not possible assign to nothing:
df.A = df.A.where(~df.A.duplicated(), '')
print (df)
A B C
0 1 a b
1 b c
2 e f
3 2 d g
4 x y
print (df.A.tolist())
[1, '', '', 2, '']
Seems MultiIndex should be way, but it only not display repeated values by default:
df = df.set_index(['A','B'])
print (df)
C
A B
1 a b
b c
e f
2 d g
x y
print (df.index)
MultiIndex([(1, 'a'),
(1, 'b'),
(1, 'e'),
(2, 'd'),
(2, 'x')],
names=['A', 'B'])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
