简体   繁体   English

如果名称是循环中的变量,如何按名称删除数据框列?

[英]How can I delete a dataframe column by name if the name is a variable in a loop?

I have a loop I created to iterate through the column names and delete them in some cases.我创建了一个循环来遍历列名并在某些情况下删除它们。 I'm trying this code below but it won't work.我正在尝试下面的代码,但它不起作用。 I'm unable to use call the dataframe column based on a variable name.我无法使用基于变量名称调用数据框列。 It's expecting the actual name.它正在等待实际名称。 Is there any way around this?有没有办法解决?

for name, values in (df.iteritems())
     .....
     .....
     del df[name]

Use drop function使用拖放功能

list =['list of columns']

for col in list:
    df.drop(col,axis=1,inplace=True)

暂无
暂无

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

相关问题 如何在不使用 for 循环的情况下检索 dataframe 中具有明确列名的每一行并写入“Hi 5”? - how can i retrieve every row in a dataframe with a definite column name and write “Hi 5 ” without using a for loop? 如何使用列名作为变量基于空/非空过滤数据框? - How can I filter dataframe based on null/not null using a column name as a variable? 如何使用 dataframe 的 column_name 作为行上的值? - How can I use the column_name of a dataframe as a value on the rows? 如何在熊猫数据框中引用以数字作为名称的列? - How can I refer to a column with a number as its name in a pandas dataframe? 如何删除 Pandas 数据框的第一列,列名未知 - How do I delete the first column of a pandas dataframe, the name of the column is unknown 我如何获取两个列表 Pandas DataFrame 列名,并且只使用一个列表,但 append 一个循环中的字符串应用于列名? - How can I take two lists of Pandas DataFrame columns names, and only use one list, but append a string in a loop to be applied to the column name? 按列名作为变量查询数据框 - Query dataframe by column name as a variable 如何按名称作为变量访问列以使用 isin() 方法 - How can I Access a Column by Name as a Variable to use the isin() Method 如何将原来的 dataframe 列名称更改为新名称? - How do I change the original dataframe column name to a new name? 如何打印 Dataframe 的名称并检查 Python 中 Dataframe 中的行和列? - How can I print the name of Dataframe and check row and column in Dataframe in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM