Find column whose name contains a specific string

erikfas

I have a dataframe with column names, and I want to find the one that contains a certain string, but does not exactly match it. I'm searching for 'spike' in column names like 'spike-2', 'hey spike', 'spiked-in' (the 'spike' part is always continuous).

I want the column name to be returned as a string or a variable, so I access the column later with df['name'] or df[name] as normal. I've tried to find ways to do this, to no avail. Any tips?

Alvaro Fuentes

Just iterate over DataFrame.columns, now this is an example in which you will end up with a list of column names that match:

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)

Output:

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

Explanation:

  1. df.columns returns a list of column names
  2. [col for col in df.columns if 'spike' in col] iterates over the list df.columns with the variable col and adds it to the resulting list if col contains 'spike'. This syntax is list comprehension.

If you only want the resulting data set with the columns that match you can do this:

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

Output:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Drop columns whose name contains a specific string from pandas DataFrame

From Dev

Find a column that contains a specific string from another column

From Dev

How do I find the location of all files with a particular name whose content contains a particular string?

From Dev

How to get list of all files from a directory (including its sub-directories) whose file name contains a specific string

From Dev

Is it possible to apply CSS to an element whose name contains a particular string?

From Dev

Get list of subdirectories which contain a file whose name contains a string

From Dev

Is it possible to apply CSS to an element whose name contains a particular string?

From Dev

jquery find a selector whose id contains a string using regex

From Dev

find all files and lines that contains a specific string

From Dev

How to find if a string contains a specific integer php

From Dev

Test if column name contains string in R

From Dev

Mongoid, find models which name contains string

From Dev

Use ransack to find objects whose array attribute contain a specific string

From Dev

In R, find the column that contains a string in for each row

From Dev

how to replace a string in files only under some folders whose name contains certain string

From Dev

Need to find specific column name from database

From Dev

Find name of directories that don't contains specific subdirectories

From Dev

how to find if a string contains numbers followed by a specific string

From Java

XPath to text node whose ancestor has a descendant that contains specific text string

From Dev

How to download all videos frome a youtube channel whose name contains a string?

From Dev

How to get the column name with a specific string?

From Dev

Reference to a bash variable whose name contains dot

From Dev

Find Column Name in Excel sheet if string matches

From Dev

SQL - select rows that their child rows's column contains specific string

From Dev

Moving row values contains specific string to new column in Python

From Dev

Pyspark - How to sum columns when column contains specific string

From Dev

SQL - select rows that their child rows's column contains specific string

From Java

How to find if an array contains a specific string in JavaScript/jQuery?

From Dev

Bash script, find all the files in current folder that contains specific string

Related Related

  1. 1

    Drop columns whose name contains a specific string from pandas DataFrame

  2. 2

    Find a column that contains a specific string from another column

  3. 3

    How do I find the location of all files with a particular name whose content contains a particular string?

  4. 4

    How to get list of all files from a directory (including its sub-directories) whose file name contains a specific string

  5. 5

    Is it possible to apply CSS to an element whose name contains a particular string?

  6. 6

    Get list of subdirectories which contain a file whose name contains a string

  7. 7

    Is it possible to apply CSS to an element whose name contains a particular string?

  8. 8

    jquery find a selector whose id contains a string using regex

  9. 9

    find all files and lines that contains a specific string

  10. 10

    How to find if a string contains a specific integer php

  11. 11

    Test if column name contains string in R

  12. 12

    Mongoid, find models which name contains string

  13. 13

    Use ransack to find objects whose array attribute contain a specific string

  14. 14

    In R, find the column that contains a string in for each row

  15. 15

    how to replace a string in files only under some folders whose name contains certain string

  16. 16

    Need to find specific column name from database

  17. 17

    Find name of directories that don't contains specific subdirectories

  18. 18

    how to find if a string contains numbers followed by a specific string

  19. 19

    XPath to text node whose ancestor has a descendant that contains specific text string

  20. 20

    How to download all videos frome a youtube channel whose name contains a string?

  21. 21

    How to get the column name with a specific string?

  22. 22

    Reference to a bash variable whose name contains dot

  23. 23

    Find Column Name in Excel sheet if string matches

  24. 24

    SQL - select rows that their child rows's column contains specific string

  25. 25

    Moving row values contains specific string to new column in Python

  26. 26

    Pyspark - How to sum columns when column contains specific string

  27. 27

    SQL - select rows that their child rows's column contains specific string

  28. 28

    How to find if an array contains a specific string in JavaScript/jQuery?

  29. 29

    Bash script, find all the files in current folder that contains specific string

HotTag

Archive