So this link (Deleting DataFrame row in Pandas based on column value) describes how to do a very simple column deletion based on a value being present...
...This line will delete rows where the column line_race
contains the value 0
:
df = df[df.line_race != 0]
My question is a little more in depth though.
I would like to delete all the rows in my DataFrame where the value in the first column is NOT a certain value. Specifically, if the first column fish_frame[0]
contains a string that doesn't match a value from another list stocks
, then delete it.
I thought something like this would do the trick, but it didn't:
fish_frame = fish_frame[fish_frame[0] == (any s in fish_frame[0] for s in stocks)]
The first column of my DataFrame, fish_frame[0]
:
0 NEFS X Available stocks
1 11/6/2013
2 Chris King
3 Package $4000 or BO
4 GOM Winter Flounder
5 HAD GBW
6 POLLOCK
7 WINTER FL SNE
8 COD GBW
9 WHITEHAKE
10 HAD GBE
11 WINTER FL GBbb
12 Grey Sole
13 YELLOWTAIL SNE
14 YELLOWTAIL GB
15 REDFISH
16 NaN
17 List A
18 Package $10,000 or BO
19 GOM Winter Flounder
And my list stocks
:
(
'GB COD EAST',
'GB COD EAST ACE',
'GB COD WEST ACE',
'GBE COD',
'GB COD WEST',
'GBW COD',
'GOM COD',
'GB HADDOCK EAST',
'GBE HADDOCK',
'GBW HADDOCK',
'GBE HADD',
'GB HADDOCK WEST',
'GBYT',
'GB YT',
'GBW HADD',
'GOM HADDOCK',
'GOM HADD',
'GOM HAD',
'GOM HADOCK',
'PLAICE',
'DABS',
'POLLOCK',
'POLL',
'REDFISH',
'REDS',
'RED',
'WHITE HAKE' ,
'WHITEHAKE',
'WHAKE',
'WHAK',
'GB WINTER FLOUNDER',
'GB WINTER FL',
'GB BB',
'GB WINTER',
'GB BLACK BACKS',
'GB BLACKBACKS',
'GOM WINTER FLOUNDER',
'WINTER GOM FLOUNDER',
'GOM BLACKBACKS',
'GOM BB',
'GOM WINTER',
'SNE WINTER FLOUNDER',
'SNE WINTER',
'SNE/MA WINTER FLOUNDER',
'SNE/MA YELLOWTAIL',
'SNE BLACKBACK',
'SNE BLACKBACKS',
'SNE BB',
'WITCH FLOUNDER',
'WITCH FL',
'WITCH',
'WHICH',
'WHITCH',
'GREYSOLE',
'GREY SOLE',
'CC/GOM YELLOWTAIL FLOUNDER',
'GOM YELLOWTAIL',
'GOM YELLOW TAIL',
'GOM YT',
'GB YELLOWTAIL FLOUNDER',
'GB YELLOWTAIL',
'GB YT',
'SNE/MA YELLOWTAIL FLOUNDER',
'SNE YT',
'SNE YELLOWTAIL',
'SNE YELLOW TAIL',
'SCALLOP IFQ'
)
Any help solving this would be appreciated, thanks.