Pandas 在多个条件下删除行
Pandas drop rows on multiple conditions
我有一个数据框:
from_dict = {'weekday': {1937: 'Thursday',
1938: 'Thursday',
1939: 'Thursday',
1940: 'Friday',
1941: 'Friday',
1942: 'Friday',
1943: 'Saturday'},
'sort': {1937: 'pre',
1938: 'day',
1939: 'twi',
1940: 'pre',
1941: 'day',
1942: 'twi',
1943: 'pre'},
'volume': {1937: 48840,
1938: 43936,
1939: 48393,
1940: 47675,
1941: 42271,
1942: 46270,
1943: 28721}}
df = pd.DataFrame.from_dict(from_dict)
weekday sort volume
1937 Thursday pre 48840
1938 Thursday day 43936
1939 Thursday twi 48393
1940 Friday pre 47675
1941 Friday day 42271
1942 Friday twi 46270
1943 Saturday pre 28721
我只想切出 'weekday' == 'Friday' & 'sort' == 'day' 的行和 'weekday' == [= 的行28=] & 'sort' == 'day'
weekday sort volume
1937 Thursday pre 48840
1939 Thursday twi 48393
1940 Friday pre 47675
1942 Friday twi 46270
1943 Saturday pre 28721
我更喜欢对行进行切片的原因是因为这个数据框被传递给 pivot_table 并且我不关心创建新变量或修改原始数据框。我尝试过按多个条件进行切片,但显然它没有用。
sort_df[((sort_df['weekday'] != 'Thursday') & (sort_df['sort'] != 'day'))]
这只会删除星期四和星期几的所有行。我知道答案会非常明显,我现在肯定知道了。
尝试:
>>> df[~(df["weekday"].isin(["Thursday", "Friday"])&df["sort"].eq("day"))]
weekday sort volume
1937 Thursday pre 48840
1939 Thursday twi 48393
1940 Friday pre 47675
1942 Friday twi 46270
1943 Saturday pre 28721
我有一个数据框:
from_dict = {'weekday': {1937: 'Thursday',
1938: 'Thursday',
1939: 'Thursday',
1940: 'Friday',
1941: 'Friday',
1942: 'Friday',
1943: 'Saturday'},
'sort': {1937: 'pre',
1938: 'day',
1939: 'twi',
1940: 'pre',
1941: 'day',
1942: 'twi',
1943: 'pre'},
'volume': {1937: 48840,
1938: 43936,
1939: 48393,
1940: 47675,
1941: 42271,
1942: 46270,
1943: 28721}}
df = pd.DataFrame.from_dict(from_dict)
weekday sort volume
1937 Thursday pre 48840
1938 Thursday day 43936
1939 Thursday twi 48393
1940 Friday pre 47675
1941 Friday day 42271
1942 Friday twi 46270
1943 Saturday pre 28721
我只想切出 'weekday' == 'Friday' & 'sort' == 'day' 的行和 'weekday' == [= 的行28=] & 'sort' == 'day'
weekday sort volume
1937 Thursday pre 48840
1939 Thursday twi 48393
1940 Friday pre 47675
1942 Friday twi 46270
1943 Saturday pre 28721
我更喜欢对行进行切片的原因是因为这个数据框被传递给 pivot_table 并且我不关心创建新变量或修改原始数据框。我尝试过按多个条件进行切片,但显然它没有用。
sort_df[((sort_df['weekday'] != 'Thursday') & (sort_df['sort'] != 'day'))]
这只会删除星期四和星期几的所有行。我知道答案会非常明显,我现在肯定知道了。
尝试:
>>> df[~(df["weekday"].isin(["Thursday", "Friday"])&df["sort"].eq("day"))]
weekday sort volume
1937 Thursday pre 48840
1939 Thursday twi 48393
1940 Friday pre 47675
1942 Friday twi 46270
1943 Saturday pre 28721