简体   繁体   English

如何使用列名中的空格在 Pandas 数据框列中进行搜索

[英]How to search in a pandas dataframe column with the space in the column name

If I need to search if a value exists in a pandas data frame column , which has got a name without any spaces, then I simply do something like this如果我需要搜索一个值是否存在于 Pandas 数据框列中,该列的名称没有任何空格,那么我只需执行以下操作

if value in df.Timestamp.values

This will work if the column name is Timestamp.如果列名是 Timestamp,这将起作用。 However, I have got plenty of data with column names as 'Date Time'.但是,我有大量数据的列名称为“日期时间”。 How do I use the if in statement in that case?在这种情况下如何使用 if in 语句? If there is no easy way to check for this using the if in statement, can I search for the existence of the value in some other way?如果没有使用 if in 语句检查这一点的简单方法,我可以以其他方式搜索该值的存在吗? Note that I just need to search for the existence of the value.请注意,我只需要搜索该值是否存在。 Also, this is not an index column.此外,这不是索引列。

Thank you for any inputs感谢您的任何投入

It's better practice to use the square bracket notation:更好的做法是使用方括号表示法:

df["Date Time"].values

Which does exactly the same thing做同样的事情

There are 2 ways of indexing columns in pandas.在 Pandas 中有 2 种索引列的方法。 One is using the dot notation which you are using and the other is using square brackets.一种是使用您正在使用的点符号,另一种是使用方括号。 Both work the same way.两者的工作方式相同。

if value in df["Date Time"].values

in the case where you want to work with a column that has a header name with spaces but you don't want it changed because you may have to forward the file ...one way is to just rename it, do whatever you want with the new no-spaced-name, them rename it back...# eg to drop the rows with the value "DUMMY" in the column 'Recipient Fullname'如果您想处理标题名称带有空格的列,但您不希望更改它,因为您可能必须转发文件......一种方法是重命名它,做任何你想做的事情新的无空格名称,他们将其重命名回...# 例如,删除“收件人全名”列中值为“DUMMY”的行

df.rename(columns={'Recipient Fullname':'Recipient_Fullname'}, inplace=True) 
df = df[(df.Recipient_Fullname != "DUMMY")]   
df.rename(columns={'Recipient_Fullname':'Recipient Fullname'}, inplace=True)

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

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