我有pandas数据帧,我想在它上面执行isnull()或不是isnull()条件的查询函数:
In [67]: df_data = pd.DataFrame({'a':[1,20,None,40,50]})
In [68]: df_data
Out[68]: a
0 1.0
1 20.0
2 NaN
3 40.0
4 50.0
如果我使用此命令:
df_data.query('a isnull', engine='python')
或此命令:
df_data.query('a isnull()', engine='python')
我收到错误:
In [75]: df_data.query('a isnull', engine='python')
File "<unknown>", line 1 a isnull
SyntaxError: invalid syntax
In [76]: df_data.query('a isnull()', engine='python')
File "<unknown>", line 1 a isnull ()
SyntaxError: invalid syntax
这样做的正确方法是什么?
谢谢。
答案 0 :(得分:13)
使用...
class PageNumberView(View):
def get(self, request, *args, **kwargs):
try:
self.sort = request.GET.get("sort")
except KeyError:
self.sort = "0"
try:
self.order = request.GET.get("order")
except KeyError:
self.order = "A"
try:
self.search = self.request.GET.get("search")
except KeyError:
self.search = ""
try:
self.tag = self.request.GET.get("tag")
except KeyError:
self.tag = ""
return super(PageNumberView, self).get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
try:
pn = self.request.GET.get("page", default=None)
if pn == None:
pn = "1"
except KeyError:
pn = "1"
self.success_url = self.success_url + "?page=" + pn
try:
self.success_url = self.success_url + "&search=" + self.request.GET.get("search")
except KeyError:
pass
try:
self.success_url = self.success_url + "&tag=" + self.request.GET.get("tag")
except KeyError:
pass
return super(PageNumberView, self).post(request, *args, **kwargs)
class PageNumberMixin(CategoryListMixin):
def get_context_data(self, **kwargs):
context = super(PageNumberMixin, self).get_context_data(**kwargs)
try:
context["pn"] = self.request.GET.get("page", default=None)
if context["pn"] == None:
context["pn"] = "1"
except KeyError:
context["pn"] = "1"
return context
class BlogDetailView(PageNumberView, DetailView, SearchMixin, PageNumberMixin):
model = Blog
template_name = "blog_detail.html"
def get_object(self):
return self.model.objects.get(pk=self.request.GET.get('id'))
class CategoryListMixin(ContextMixin):
def get_context_data(self, **kwargs):
context = super(CategoryListMixin, self).get_context_data(**kwargs)
context["current_url"] = self.request.path
context["categories"] = Category.objects.all()
return context
class BlogCreate(SuccessMessageMixin, CreateView, CategoryListMixin):
model = Blog
template_name = "blog_add.html"
success_message = "Article successfully created"
fields = '__all__'
def form_valid(self, form):
form.instance.user = self.request.user
return super(BlogCreate, self).form_valid(form)
...
:
.
您也可以使用逻辑a = df_data.query('a.isnull()', engine='python')
print (a)
a
2 NaN
b = df_data.query('a.notnull()', engine='python')
print (b)
a
0 1.0
1 20.0
3 40.0
4 50.0
:
NaN != NaN