Pandas:按多列中的值过滤(Pandas: Filter by values within multiple columns)
我正在尝试根据单个条件中的多个列中的值过滤数据帧,但保留其他列,我根本不想应用过滤器。
我已经回顾了这些答案,第三个是最接近的,但仍然没有运气:
建立:
import pandas as pd df = pd.DataFrame({ 'month':[1,1,1,2,2], 'a':['A','A','A','A','NONE'], 'b':['B','B','B','B','B'], 'c':['C','C','C','NONE','NONE'] }, columns = ['month','a','b','c']) l = ['month','a','c'] df = df.loc[df['month'] == df['month'].max(), df.columns.isin(l)].reset_index(drop = True)
电流输出:
month a c 0 2 A NONE 1 2 NONE NONE
期望的输出:
month a 0 2 A 1 2 NONE
我试过了:
sub = l[1:] df = df[(df.loc[:, sub] != 'NONE').any(axis = 1)]
和许多其他变化(
.all()
,[sub, :]
,~df.loc[...]
,(axis = 0)
),但都没有运气。基本上我想删除其中包含所有“NONE”值的任何列(在
sub
列表中)。任何帮助深表感谢。
I'm trying to filter a dataframe based on the values within the multiple columns, based on a single condition, but keep other columns to which I don't want to apply the filter at all.
I've reviewed these answers, with the third being the closest, but still no luck:
- how do you filter pandas dataframes by multiple columns
- Filtering multiple columns Pandas
- Python Pandas - How to filter multiple columns by one value
Setup:
import pandas as pd df = pd.DataFrame({ 'month':[1,1,1,2,2], 'a':['A','A','A','A','NONE'], 'b':['B','B','B','B','B'], 'c':['C','C','C','NONE','NONE'] }, columns = ['month','a','b','c']) l = ['month','a','c'] df = df.loc[df['month'] == df['month'].max(), df.columns.isin(l)].reset_index(drop = True)
Current Output:
month a c 0 2 A NONE 1 2 NONE NONE
Desired Output:
month a 0 2 A 1 2 NONE
I've tried:
sub = l[1:] df = df[(df.loc[:, sub] != 'NONE').any(axis = 1)]
and many other variations (
.all()
,[sub, :]
,~df.loc[...]
,(axis = 0)
), but all with no luck.Basically I want to drop any column (within the
sub
list) that has all 'NONE' values in it.Any help is much appreciated.
原文:https://stackoverflow.com/questions/44416069
最满意答案
我将创建更多轻量级对象,如System.Windows.Media.DrawingVisual对象,并在绘图上下文中绘制矩形。 这些将托管在充当ItemsControl的Framework元素中。
这篇伟大的文章描述了如何实现有效的ItemsControl: 编写更高效的ItemsControls
I would create more lightweight objects such as System.Windows.Media.DrawingVisual objects and draw the rectangle in the drawing context. These would be hosted in a Framework element acting as an ItemsControl.
This great article describes how you could implement an efficient ItemsControl: Writing More Efficient ItemsControls
相关问答
更多-
您可以使用此代码隐藏软键盘 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 获取EditText的实例 EditText editText = (EditText) findViewById(R.id.edittext); 要防止Default SoftKeyboard出现在EditText中,请覆盖以下事件 //显示自定义键盘 edittext.setOnFoc ...
-
setImageDrawable后滞后(Lag after setImageDrawable)[2022-08-26]
我建议您使用Picaso库来加载图像,因为它非常快速有效 它很简单 Picasso.with(context).load(R.drawable.landing_screen).into(imageView1); 有关更多信息,我建议您转到Picaso文档 I suggest you to use Picaso library to load images as its very fast and efficient its as simple as Picasso.with(context).load(R ... -
我找到了解决问题的方法: 我添加了一个包含输入字段的div,这个div的z-index为-10,所以它不会出现 隐藏字段始终拥有焦点,直到选择其他输入字段。 新选择的输入字段将获得模糊功能,以将焦点恢复到隐藏字段。 隐藏的领域