Reputation: 14112
I have a df which looks like this:
Name UnweightedBase Base exp1
Name
UnweightedBase 1046 1046 1046
Base 1046 1046 1046
ppola1 15 1,28% 1,28%
ppola2 9 1,21% 1,21%
ppola3 10 1,07% 1,07%
ppola4 46 5,32% 5,32%
ppola5 171 16,91% 16,91%
ppola6 36 4,01% 4,01%
ppola7 45 3,88% 3,88%
ppola8 28 2,63% 2,63%
ppola9 4 0,29% 0,29%
ppola10 178 17,76% 17,76%
I want to build 2 seprate sub dfs based on a list of index labels. 1 which includes the labels and 1 which doesn't
Heres the list I want to check against:
fixedlist = ["ppola8", "ppola9", "ppola10"]
my attempts to create these sub dfs:
1) included = df_raw.loc[exclude_list]
2) excluded = df_raw.loc[~exclude_list]
the 1st "included" df works but the 2nd doesnt, I get the following error:
TypeError: bad operand type for unary ~: 'list'
How do I get around this?
Upvotes: 1
Views: 483
Reputation: 394179
You can't do what you attempted like that hence the error because the unary operator won't work on a list.
You can achieve what you want by doing
excluded = df_raw[~df_raw.index.isin(exclude_list)]
the inner statement will produce a boolean index that can be used to index your df.
Upvotes: 1