简体   繁体   English

选择具有数据框条件的列

[英]select columns with condition on dataframe

I have a dataframe: 我有一个数据框:

    GPA1PP GPB1PP C D E  GPAB12PP
0
1
2
3

Now I want to select some columns 现在我要选择一些列

a_test=df.loc[:,df.columns.str.contains("A")]
b_test=df.loc[:,df.columns.str.contains("B")]

1. Both a_test and b_test have "GPAB12PP" columns but I want to it only appears in the a_test dataframe. 1. a_test和b_test都有“ GPAB12PP”列,但我希望它仅出现在a_test数据框中。 How can I do? 我能怎么做?

2. 2。

CDE columns not be selected. 未选择CDE列。 Can I use "minus" to select them? 我可以使用“减号”来选择它们吗?

For example in R 例如在R中

c_test=df[:,-c(1,2,6)]
print(c_test)
  C D E
0
1
2
3

Does python be the same? python是一样的吗?

Thanks 谢谢

  1. for the first question you can use this: 对于第一个问题,您可以使用以下命令:

     a_test=df.loc[:,df.columns.str.startswith("A")] b_test=df.loc[:,df.columns.str.startswith("B")] 
  2. for the second question you can use this: 对于第二个问题,您可以使用以下命令:

     c_test=df.loc[:,~(df.columns.str.contains('A') | df.columns.str.contains('B'))] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM