I have a dataframe df_alltrades with a number of similar columns where the string includes integers:
instrument Bid0Mkt Bid1Mkt Bid2Mkt Bid3Mkt Bid4Mkt ...
0 EURUSD 1.1 1.2 1.2 1.3 1.3
1 NZDUSD 0.6 0.65 0.7 0.9 0.92
. . . . . .
. . . . . .
I also have corresponding columns for OfferXMkt and a number of other columns formatted as str.
I want to remove any columns where X in BidXMkt and OfferXMkt is greater than 0 so that I end up with something like:
instrument Bid0Mkt Offer0Mkt ...
0 EURUSD 1.1
1 NZDUSD 0.6
. . .
. . .
I can do this by writing out each if statement
for column in df_alltrades.columns:
if '1' in column:
df_alltrades.drop(columns=column, inplace=True)
if '2' in column:
df_alltrades.drop(columns=column, inplace=True)
if '3' in column:
df_alltrades.drop(columns=column, inplace=True)
if '4' in column:
df_alltrades.drop(columns=column, inplace=True)
but I was hoping to clean it up with a loop, something like:
for column in df_alltrades.columns:
for C in range(1, 5):
if C in column:
df_alltrades.drop(columns=column, inplace=True)
but I'm getting the error:
TypeError: 'in <string>' requires string as left operand, not int
I have also tried things like;
for X in range(1, 5):
BidMkt = 'Bid{}Mkt'.format(X)
df_new = df_alltrades.drop([BidMkt], axis = 1)
which even though print(BidMkt) gives the full list, only Bid4Mkt is dropped.
Thanks for any help!