Reputation: 205
I have a very big dataset and i am working by selecting subset of the entire data in each of these subset i want to drop all the subsequent columns after matching one of the column name's string value that is randomnr.
my df columns looks like this:-
A B C D E randomnr H I J K
If this is the subset i am working on i want to drop H I J K columns which are after my common string randomnr and this is the common string match for all the subsets. for example:- in first subset it can be 'randomnr_abc' and in second subset 'randomnr_123' but all subsets contains 'randomnr'
I am specifically looking to drop these columns for the subset i am working on so that i can use the same code for all the other subsets. Please help me on this. thanks in advance
Upvotes: 0
Views: 998
Reputation: 29742
IIUC, use pandas.Index.str.find
with argmax
(assuming your keyword exists uniquely:
print(df.iloc[:, :df.columns.str.find("randomnr").argmax()+1])
Sample:
# df
A B C D E randomnr_123 H I J K
0 1 2 3 4 5 6 7 8 9 10
# df2
A B randomnr_abc H I J K
0 1 2 6 7 8 9 10
Output:
print(df.iloc[:, :df.columns.str.find("randomnr").argmax()+1])
A B C D E randomnr_123
0 1 2 3 4 5 6
print(df2.iloc[:, :df2.columns.str.find("randomnr").argmax()+1])
A B randomnr_abc
0 1 2 6
Upvotes: 3