查找名称包含特定字符串的列

时间:2014-01-22 14:17:43

标签: python python-3.x string pandas dataframe

我有一个带有列名的数据框,我想找到一个包含某个字符串的字符串,但不完全匹配它。我在'spike''spike-2''hey spike'等列名中搜索'spiked-in''spike'部分始终是连续的)。

我希望将列名作为字符串或变量返回,因此我稍后会使用df['name']df[name]正常访问该列。我试图找到办法做到这一点,但无济于事。有什么提示吗?

7 个答案:

答案 0 :(得分:159)

只需迭代DataFrame.columns,现在这是一个示例,您将在其中找到匹配的列名列表:

import pandas as pd

data = {'spike-2': [1,2,3], 'hey spke': [4,5,6], 'spiked-in': [7,8,9], 'no': [10,11,12]}
df = pd.DataFrame(data)

spike_cols = [col for col in df.columns if 'spike' in col]
print(list(df.columns))
print(spike_cols)

输出:

['hey spke', 'no', 'spike-2', 'spiked-in']
['spike-2', 'spiked-in']

说明:

  1. df.columns返回列名列表
  2. [col for col in df.columns if 'spike' in col]使用变量df.columns对列表col进行迭代,如果col包含'spike',则将其添加到结果列表中。此语法为list comprehension
  3. 如果您只希望结果数据集包含与您匹配的列,则可以执行以下操作:

    df2 = df.filter(regex='spike')
    print(df2)
    

    输出:

       spike-2  spiked-in
    0        1          7
    1        2          8
    2        3          9
    

答案 1 :(得分:37)

This answer使用DataFrame.filter方法在没有列表理解的情况下执行此操作:

import pandas as pd

data = {'spike-2': [1,2,3], 'hey spke': [4,5,6]}
df = pd.DataFrame(data)

print(df.filter(like='spike').columns)

只输出#spike-2'。您也可以使用正则表达式,正如有些人在上面的评论中所建议的那样:

print(df.filter(regex='spike|spke').columns)

将同时输出两列:[' spike-2',' hey spke']

答案 2 :(得分:10)

您也可以使用df.columns[df.columns.str.contains(pat = 'spike')]

data = {'spike-2': [1,2,3], 'hey spke': [4,5,6], 'spiked-in': [7,8,9], 'no': [10,11,12]}
df = pd.DataFrame(data)

colNames = df.columns[df.columns.str.contains(pat = 'spike')] 

print(colNames)

这将输出列名:'spike-2', u'spiked-in'

有关pandas.Series.str.contains的更多信息。

答案 3 :(得分:4)

df.loc[:,df.columns.str.contains("spike")]

答案 4 :(得分:3)

# select columns containing 'spike'
df.filter(like='spike', axis=1)

您也可以按名称选择正则表达式。请参阅:pandas.DataFrame.filter

答案 5 :(得分:0)

您还可以使用以下代码:

spike_cols =[x for x in df.columns[df.columns.str.contains('spike')]]

答案 6 :(得分:0)

根据“开始”,“包含”和“结束”获取名称和子集:

# from: https://stackoverflow.com/questions/21285380/find-column-whose-name-contains-a-specific-string
# from: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.str.contains.html
# from: https://cmdlinetips.com/2019/04/how-to-select-columns-using-prefix-suffix-of-column-names-in-pandas/
# from: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.filter.html




import pandas as pd



data = {'spike_starts': [1,2,3], 'ends_spike_starts': [4,5,6], 'ends_spike': [7,8,9], 'not': [10,11,12]}
df = pd.DataFrame(data)



print("\n")
print("----------------------------------------")
colNames_contains = df.columns[df.columns.str.contains(pat = 'spike')].tolist() 
print("Contains")
print(colNames_contains)



print("\n")
print("----------------------------------------")
colNames_starts = df.columns[df.columns.str.contains(pat = '^spike')].tolist() 
print("Starts")
print(colNames_starts)



print("\n")
print("----------------------------------------")
colNames_ends = df.columns[df.columns.str.contains(pat = 'spike$')].tolist() 
print("Ends")
print(colNames_ends)



print("\n")
print("----------------------------------------")
df_subset_start = df.filter(regex='^spike',axis=1)
print("Starts")
print(df_subset_start)



print("\n")
print("----------------------------------------")
df_subset_contains = df.filter(regex='spike',axis=1)
print("Contains")
print(df_subset_contains)



print("\n")
print("----------------------------------------")
df_subset_ends = df.filter(regex='spike$',axis=1)
print("Ends")
print(df_subset_ends)