python用DataFrame筛选含有某些字段的行(contains和isin)| 含有nan的行

  • Post author:
  • Post category:python

1. python用dataframe筛选含有某些字段的行(contains和isin)

import pandas as pd
df = {'DataBase':['mysql','test','test','test','test'],'table':['user','student','course','sc','book']}
df = pd.DataFrame(df)
print(df)
print("-"*12)
print('================')
list_one=['student','sc']
print(type(df['table']))
a=df[(df['table'].isin(list_one))]
print(a)

在这里插入图片描述

2. 另外一个筛选字符的(含有的形式)

import pandas as pd
df = {'地址':['北京','上海','长沙','北京省会','广州市区'],'table':['user','student','course','sc','book']}
df = pd.DataFrame(df)
print(df)
print('================')
citys = ['北京', '天津', '上海']
address = '|'.join(citys)
df_new = df[df['地址'].str.contains(address)]
print(df_new)

在这里插入图片描述

3. 查看有nan的行,和不含有nan的行

#1 查看含有nan的行

df[df.isna().any(axis=1)]

#2, 查看不含有nan的行

df[~df.isna().any(axis=1)]