find a string that contains specific value

Related searches

How can I print all the dates in the dataframe (parsed to a string) which contain 2012-06 in the string

I dont remember which character symbolizes "ok after me are random characters, this part is not important"

i thought it was (.*) but its not

the purpose of this is to find and open all CSV files. so I thought I could get away with something like this in the end : 2012-06(.*).csv to open all june 2012 files and "do stuff"

import pandas as pd
from datetime import timedelta

datelist = pd.date_range(pd.datetime(year = 2012, month = 6, day = 15), pd.datetime.today()).tolist()
df = pd.DataFrame(datelist)

for date in df[0]:

  d = str(date)

  if d == "2012-06(.*)":  

    print(d)  

You can use glob (https://docs.python.org/3/library/glob.html) to use 2012-06-* pattern.

For example :

Let's say you have these files in a folder :

ls -l files
total 0
-rw-r--r--  1 julien  staff  0 Apr 30 13:57 2012-06-01_test.csv
-rw-r--r--  1 julien  staff  0 Apr 30 13:58 2012-06-15_my_file.csv
-rw-r--r--  1 julien  staff  0 Apr 30 13:58 2013-01-10_my_file.csv

You can do this :

import glob

files = glob.glob('files/2012-06*.csv')
print(files)

It will print :

['files/2012-06-15_my_file.csv', 'files/2012-06-01_test.csv']

With this list files you can iterate over and open each of them for example

Excel formula: If cell contains, To find the position of a substring in a text string, use the SEARCH function. Add the ISNUMBER function. You can also check if a cell contains specific text, without displaying the substring. To perform a case-sensitive search, replace the SEARCH function with the FIND function. Add the IF function. To check if a cell contains specific text, you can use the SEARCH function together with the ISNUMBER function. In the generic version, substring is the specific text you are looking for, and text represents text in the cell you are testing. In the example shown, the formula in D5 is: = ISNUMBER(SEARCH(C5, B5))

I suggest use Series.str.contains:

df1 = df[df[0].astype(str).str.contains("2012-06")]

Or Series.str.startswith for filtering in pandas:

df1 = df[df[0].astype(str).str.startswith("2012-06")]

print (df1)

            0
0  2012-06-15
1  2012-06-16
2  2012-06-17
3  2012-06-18
4  2012-06-19
5  2012-06-20
6  2012-06-21
7  2012-06-22
8  2012-06-23
9  2012-06-24
10 2012-06-25
11 2012-06-26
12 2012-06-27
13 2012-06-28
14 2012-06-29
15 2012-06-30

Contains Specific Text, function which is used to find the occurrence of one string inside another one: returns true if $needle is a substring of $haystack function contains($needle, to the left has the same value as the variable / expression / constant to the right. The Java String contains () method is used to check whether the specific set of characters are part of the given string or not. It returns a boolean value true if the specified characters are substring of a given string and returns false otherwise. It can be directly used inside the if statement. Syntax of String "Contain" method

You can also query by year and month.

Ex:

import pandas as pd

datelist = pd.date_range(pd.datetime(year = 2012, month = 6, day = 15), pd.datetime.today()).tolist()
df = pd.DataFrame(datelist)
print(df[(df[0].dt.year == 2012) & (df[0].dt.month == 6)])

How do I check if a string contains a specific word?, ISNUMBER function is used to check the cell if it contains a number or not. Syntax of ISNUMBER: =ISNUMBER (value). The FIND function returns the position of� Excel SEARCH function. The Excel SEARCH function returns the number of the starting location of a substring in a text string.The syntax of the SEARCH function is as below:= SEARCH (find_text, within_text, [start_num])…. Excel ISNUMBER function.

Searching a String for a Specific Substring in Excel, If we need to find a text string in Excel, if cell contains the same text. Use a You don't need to filter your data to count specific values. Countif function is� We can return some value if cell contains some string. Here is the the the Excel formula to return a value if a Cell contains Text. You can check a cell if there is given string in the Cell and return some string or value in another column. =IF(ISNUMBER(SEARCH(“How”,A1,1)),”Found”,”Not Found”)

How to lookup cells having certain text and returns the Certain Text , The VBA Instr Function checks if a string of text is found in another string of text. It returns 0 if the text is not found. Otherwise it returns the character position where the text is found. The Instr Function performs exact matches.

To sum cells when certain values are not blank, you can use the SUMIF function. In the example shown, cell G6 contains this formula: = SUMIF ( C5:C11 , "<>" , D5:D11 ) This formula sums the amounts in column D only when the value in

Comments
  • so basically you're looking for all filenames (strings) containing another string? why not if your_string in your_filename: etc.?
  • yeah, this seems the simplest solution, thanks
  • this is exactly what I've been looking for.... docs.python.org/3/library/glob.html
  • Glad it helped :)