在Python中,如果你想要查询包含空格的列名,你可以使用以下方法:
1. 使用`str.contains`方法:
import pandas as pd
假设df是你的数据框,并且列名包含空格
df.columns = df.columns.str.strip() 去除列名中的空格
columns_with_spaces = df.columns[df.columns.str.contains(' ')]
print(columns_with_spaces)
2. 使用列表推导式:
import pandas as pd
假设df是你的数据框,并且列名包含空格
df.columns = df.columns.str.strip() 去除列名中的空格
columns_with_spaces = [col for col in df.columns if ' ' in col]
print(columns_with_spaces)
3. 使用正则表达式:
import pandas as pd
假设df是你的数据框,并且列名包含空格
df.columns = df.columns.str.strip() 去除列名中的空格
columns_with_spaces = df.columns[df.columns.str.match(r'\s')]
print(columns_with_spaces)
以上方法可以帮助你找到包含空格的列名。