Python 筛选行后获取空数据帧
如果列Python 筛选行后获取空数据帧,python,pandas,Python,Pandas,如果列Text中至少包含以下一个字符串,则需要筛选行: list_house=['house','apartment','home','cottage'] 我的工作如下: list_house=['house','apartment','home','cottage'] df_low=df(lambda x: x.astype(str).str.lower()).copy() df_fil=df_low[df_low['Text'].isin(list_house)] 但是,我不确定它是否
Text
中至少包含以下一个字符串,则需要筛选行:
list_house=['house','apartment','home','cottage']
我的工作如下:
list_house=['house','apartment','home','cottage']
df_low=df(lambda x: x.astype(str).str.lower()).copy()
df_fil=df_low[df_low['Text'].isin(list_house)]
但是,我不确定它是否选择了至少包含其中一个的行,或者选择了包含所有这些字符串的行(我得到的数据帧是空的,实际上我希望得到一些结果)
数据帧示例:
Text
my house is bigger than yours
I bought a small cottage a few years ago in UK
So you live in an apartment in the city centre. Lucky you! I live in a small house in the countryside.
I like your dogs.
给你:
如果存在以下其中一个词,则会过滤:
df = df[df['Text'].str.contains('|'.join(list_house))]
df = df[~df['Text'].str.contains('|'.join(list_house))]
如果其中一个词存在,则会过滤掉:
df = df[df['Text'].str.contains('|'.join(list_house))]
df = df[~df['Text'].str.contains('|'.join(list_house))]
是的,效果很好。我可以问你为什么isin不工作吗?isin-比较完整字符串而不是字符串的一部分-就像你在这里需要的那样