Home > Back-end >  Slice pandas dataframe column by start and end values
Slice pandas dataframe column by start and end values

Time:07-27

For example, I have a dataframe that looks like this:

0       -- end
1       QQQQ
2       GEO
3       DEF
4       ABC
5       -- start
6       -- end
7       apple
8.      -- start

Is it possible to dynamically slice the column by the '-- end' & '-- start'. Meaning, I want to work with the data between the -- start and -- end independently.

start_end = df[df.col.str.contains('-- end') 1:df.col.str.contains('-- start')]

To no avail, maybe this isn't even possible in pandas but would love input.

Thank you all.

CodePudding user response:

You could try as follows:

data = {'column': {0: '-- end',
  1: 'QQQQ',
  2: 'GEO',
  3: 'DEF',
  4: 'ABC',
  5: '-- start',
  6: '-- end',
  7: 'apple',
  8: '-- start'}}

df = pd.DataFrame(data)

exclude_lst = ['-- start','-- end']

# get False for members of exclude_lst, True for the rest
bools = ~df.column.isin(['-- start','-- end'])

# get sequences: [1, 2, 2, 2, 2, 3, 3, 4, 5]
sequences = (bools != bools.shift()).cumsum()

# keep only sequences where bools == True (so, only 2 and 4)
groups = df[bools].groupby([sequences])

# now you can loop through each slice, and perform some operation on them
for gr in groups:
    print(gr)
    
# or put them in a list and go from there:
gr_lst = list(groups)

print(gr_lst[0])

(2,   column
1   QQQQ
2    GEO
3    DEF
4    ABC)

# so, we end up with tuples. Here gr_lst[0][0] == 2, a ref to first slice as [2, 2, 2, 2]

# use gr_lst[i][1] to access an actual slice, e.g.:
print(gr_lst[1][1])

  column
7  apple
  • Related