我正在尝试将|
与df.loc
合并以提取数据。我写的代码提取了csv文件中的所有内容。这是原始的csv文件:https://drive.google.com/open?id=16eo29mF0pn_qNw-BGpZyVM9PBxv2aN1G
import pandas as pd
df = pd.read_csv("yelp_business.csv")
df = df.loc[(df['categories'].str.contains('chinese', case = False)) | (df['name'].str.contains('subway', case = False)) | (df['categories'].str.contains('', case = False)) | (df['address'].str.contains('', case = False))]
print df
看起来空白引用^ {< CD3>}在^ {< CD4>}中不起作用,或者{^ {CD1>}在^ {CD2>}中不起作用。它不只是返回带有chinese
餐馆(数字为4171
)的行和带有餐馆名称subway
的行,而是返回所有174,568
行
编辑
我想要的输出应该是类别chinese
的所有行和名称subway
的所有行,同时考虑到地址可能没有任何赋值或为null
import pandas as pd
df = pd.read_csv("yelp_business.csv")
cusine = 'chinese'
name = 'subway'
address #address has no assigned value or is NULL
df = df.loc[(df['categories'].str.contains(cusine, case = False)) |
(df['name'].str.contains(name, case = False)) |
(df['address'].str.contains(address, case = False))]
print df
这段代码给了我一个错误NameError: name 'address' is not defined
有关包含的详细信息,请参阅 https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.contains.html
我认为这里有可能的链式条件是
|
对于categories
列,对于查找空字符串使用^""$
-它用引号匹配字符串的开头和结尾:编辑:如果需要过滤空值和NaNs值:
相关问题 更多 >
编程相关推荐