Python Forum
How to get first and last row index of each unique names in pandas dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get first and last row index of each unique names in pandas dataframe
#1
I am very new to python, I want to get the first and last row index of each unique elements based on the 2nd column.
MUY09KT00    TW00.00
MUY09KT00    TW00.00
MUY09KT00    TW00.00
MHJ09KT00    PW00.00
MHJ09KT00    PW00.00
LHJ09KT00    NPW00.00
LHJ09KT00    NPW00.00
LHJ09KT00    NPW00.00
in this case, the unique element first index are:
1
4
6
in this case, the unique element last index are:
3
5
8
Reply
#2
NB! I didn't notice that it was about pandas dataframe. Therefore following is not applicable solution.

One way of doing it is below. However, it's unclear what 'unique' means. Following code assumes that rows are ordered and only change in items must be recorded. There was no requirements in which data structure result must be stored so I used dictionary (if items are unordered then only last occurrence is preserved). It works only on Python 3.6+ because it relies on fact that dictionaries are insertion ordered.

I added one row to example data to show how one ocurrance is recorded

lst = ['MUY09KT00    TW00.00',
       'MUY09KT00    TW00.00',
       'MUY09KT00    TW00.00',
       'MHJ09KT00    PW00.00',
       'MHJ09KT00    PW00.00',
       'LHJ09KT00    NPW00.00',
       'LHJ09KT00    NPW00.00',
       'LHJ09KT00    NPW00.00',
       'PHJ09KT00    PPW00.00',
       ]

for i, row in enumerate(lst, start=1):
    item = row.split()[1]
    if i == 1:
        d = {item: [i]}                         # create dictionary and first item with start index
        
        
    else:
        if item != prev_item:                   # if item changes
            d[list(d.keys())[-1]].append(i-1)   # add ending index to item
            d[item] = [i]                       # add new item with start index
            
            
        if i == len(lst):                       # last row
            d[item].append(i)
    prev_item = item

print(d)
{'TW00.00': [1, 3], 'PW00.00': [4, 5], 'NPW00.00': [6, 8], 'PPW00.00': [9, 9]}
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create dataframe from the unique data of two dataframes Calab 6 977 Mar-02-2025, 01:51 PM
Last Post: Pedroski55
Question [Solved] Formatting cells of a pandas dataframe into an OpenDocument ods spreadsheet Calab 1 674 Mar-01-2025, 04:51 AM
Last Post: Calab
  Find duplicates in a pandas dataframe list column on other rows Calab 2 2,190 Sep-18-2024, 07:38 PM
Last Post: Calab
  Find strings by index from a list of indexes in a different Pandas dataframe column Calab 3 1,630 Aug-26-2024, 04:52 PM
Last Post: Calab
  Add NER output to pandas dataframe dg3000 0 1,157 Apr-22-2024, 08:14 PM
Last Post: dg3000
  Grouping in pandas/multi-index data frame Aleqsie 3 2,303 Jan-06-2024, 03:55 PM
Last Post: deanhystad
  HTML Decoder pandas dataframe column mbrown009 3 2,692 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 2,018 Jan-06-2023, 10:53 PM
Last Post: haihal
  Separating unique, stable, samples using pandas keithpfio 1 1,743 Jun-20-2022, 07:06 PM
Last Post: keithpfio
  Pandas Dataframe Filtering based on rows mvdlm 0 2,082 Apr-02-2022, 06:39 PM
Last Post: mvdlm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020