I have a table shown below that I would like to separate into two. The catch is the number of rows varies and I need the table to separate at the third row that contains the string 'CPT'. This row is not always the 10th row. The table is represented by the dataframe dfm
. I want to be able to create two new dataframes, one containing the rows above the third row containing 'CPT' and one containing the rows below the third row containing 'CPT'
I have the following code so far to identify all the rows and specifically the third row containing 'CPT' but I'm not sure if I'm on the right track.
dfm.columns
i= dfm.index[dfm['LABEL(click to hide)'] == 'CPT'].tolist()
i
i[-1]
Filter_dfm = dfm[dfm.index.isin(i)]
Filter_dfm
Edit: I used the following code to split the dataframe, dfm, into multiple dataframes at the rows containing 'CPT'
dfm.rename(columns = {'LABEL(click to hide)' : 'ExSD', '(I)' : 'Risk'}, inplace = True)
m = dfm.ExSD.str.contains('CPT').cumsum()
d = {f'dfm{i}': g for i, g in dfm.groupby(m)}
for k, v in d.items():
print(k)
print(v, end='\n\n')
Then to return the data frames I wanted I used Input:
d['dfm2']
Output:
ExSD Risk
1 CPT % Utilization Cumulative (With No New Charge) ...
2 05/1622:00 115.97 %
3 05/1700:45 37.3 %
4 05/1705:00 34.21 %
5 05/1712:00 22.27 %
6 05/1714:00 30.01 %
7 05/1716:00 33.93 %
8 05/1717:00 42.04 %
9 05/1718:00 46.48 %
Input:
d['dfm3']
Output:
ExSD Risk
10 CPT % Utilization Cumulative (With No New Charge) ...
11 05/1622:00 8.24 %
12 05/1700:45 53.05 %
13 05/1705:00 36.04 %
14 05/1712:00 21.21 %
15 05/1714:00 20.65 %
16 05/1716:00 21.13 %
17 05/1717:00 23.09 %
18 05/1718:00 23.46 %