首页 \ 问答 \ Pandas:按多列中的值过滤(Pandas: Filter by values within multiple columns)

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:

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
更新时间:2022-08-28 14:08

最满意答案

我将创建更多轻量级对象,如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 ...
  • 我建议您使用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,所以它不会出现 隐藏字段始终拥有焦点,直到选择其他输入字段。 新选择的输入字段将获得模糊功能,以将焦点恢复到隐藏字段。 隐藏的领域
    使用负数显式设置为在顺序焦点导航之外(根据HTML 5 ) 在目标元素或其前面的元素上调用AnHTMLElementNode.blur() 以这样的方式调用AnHTMLElementNode.focus() ,使得另一个元素获得焦点作为对在顺序导航中到达目标元素必须完成的事情的反应 使用通常不执行某些操作的元素(例如 )并向其添加onclick事件。 Explicitly setting it to be outside sequential focus navigation by using ...
  • 我将创建更多轻量级对象,如System.Windows.Media.DrawingVisual对象,并在绘图上下文中绘制矩形。 这些将托管在充当ItemsControl的Framework元素中。 这篇伟大的文章描述了如何实现有效的ItemsControl: 编写更高效的ItemsControls I would create more lightweight objects such as System.Windows.Media.DrawingVisual objects and draw the re ...
  • 看起来可以在MouseUp事件中更改焦点。 我想如果你做得太早,就像在GotKeyboardFocus事件中一样,你会在listview处理事件并选择所选项目之前窃取焦点。
  • 删除android:focusable属性,android:foucsableInTouchMode属性和XML文件中的标签,立即行动,然后 public class YOURMAINActivity extends Activity implements OnClickListener { private EditText editTxt1; private EditText editTxt2; @Override public void onCreate(Bundle sav ...
  • 我仍然不确定为什么会这样,但这是我找到的解决方案: 在窗口上设置Focusable="False"和FocusManager.IsFocusScope="False" 。 在控件上设置Focusable="True"和FocusManager.IsFocusScope="True" 。 三。 使用以下内容: PleasedPanda.LostKeyboardFocus += (sender, e) => { if(e.NewFocus == null) { Keyboard. ...
  • 来自Microsoft文档。 当按下其中一个导航键时,KeyboardNavigation类负责实现默认键盘焦点导航。 导航键是:TAB,SHIFT + TAB,CTRL + TAB,CTRL + SHIFT + TAB,UPARROW,DOWNARROW,LEFTARROW和RIGHTARROW键。 通过设置附加的KeyboardNavigation属性TabNavigation,ControlTabNavigation和DirectionalNavigation,可以更改导航容器的导航行为。 这些属性的 ...
  • 发生这种情况是因为您在SearchDisplay委托中执行需要花费时间的操作。 你应该检查searchResults大小 searchResults = [chengduhua filteredArrayUsingPredicate:resultPredicate]; 尝试限制结果。 This is happening because you are performing operations in SearchDisplay delegate that are taking time. You shou ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)