How to select a portion of dataframe as new dataframe in Pandas?

Viewed 37

Sample Dataframe

here is the image of the original dataframe in pandas. But i want to slice the dataframe based on the location of "START" value in the dataframe.

I would want to search the entire data frame for 'START' and then select all values after START as the column ex. MONTH1, MONTH2, MONTH3 ....until data extends( i want to select range from location of "START" +1 till end) as columns of the new data frame and 630,559,994,501 ....... would be rows till the end of the new data frame.

output final dataframe to be as below

final data frame

1 Answers

use .index to find row number of "START" string, later use .iloc to slice dataframe:

idx = df.index.get_loc(df[df['Column_Name'] == 'START'].index[0]) # case sensitive
sub_df= df.iloc[idx:,1:]

Update, as per your comment: if column name is not known, but you know it's location (number), use .iloc to select column by it's number

idx = df.index.get_loc(df[df.iloc[:,0] == 'START'].index[0]) # case sensitive
Related