Issue
I have the pandas dataframe as below
A B C
Apple 20 A1
Apple 30 A2
Apple 40 A3
Kiwi 20 K1
Kiwi 30 K2
Kiwi 10 K3
I want the output as
A B C
Apple 20 A1
30 A2
40 A3
Kiwi 20 K1
30 K2
10 K3
Solution
Use Groupby.cumcount
with df.loc
and Series.ne
:
In [938]: df.loc[df.groupby('A').cumcount().ne(0), 'A'] = ''
In [939]: df
Out[939]:
A B C
0 Apple 20 A1
1 30 A2
2 40 A3
3 Kiwi 20 K1
4 30 K2
5 10 K3
Answered By - Mayank Porwal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.