简体   繁体   English

根据列名在两列之间删除熊猫数据框中的多列

[英]Dropping multiple columns in a pandas dataframe between two columns based on column names

A super simple question, for which I cannot find an answer.一个超级简单的问题,我找不到答案。

I have a dataframe with 1000+ columns and cannot drop by column number, I do not know them.我有一个包含 1000 多列的数据框,不能按列号删除,我不知道。 I want to drop all columns between two columns, based on their names.我想根据名称删除两列之间的所有列。

foo = foo.drop(columns = ['columnWhatever233':'columnWhatever826']) 

does not work.不起作用。 I tried several other options, but do not see a simple solution.我尝试了其他几个选项,但没有看到简单的解决方案。 Thanks!谢谢!

You can use .loc with column range.您可以将.loc与列范围一起使用。 For example if you have this dataframe:例如,如果您有此数据框:

   A  B  C  D  E
0  1  3  3  6  0
1  2  2  4  9  1
2  3  1  5  8  4

Then to delete columns B to D :然后删除列BD

df = df.drop(columns=df.loc[:, "B":"D"].columns)
print(df)

Prints:印刷:

   A  E
0  1  0
1  2  1
2  3  4

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

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