简体   繁体   English

查找包含特定值的字符串

[英]find a string that contains specific value

How can I print all the dates in the dataframe (parsed to a string) which contain 2012-06 in the string 如何在字符串中包含2012-06的数据框中打印所有日期(解析为字符串)

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. 目的是查找并打开所有CSV文件。 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" 所以我想我最终2012-06(.*).csv这样的事情: 2012-06(.*).csv打开2012年6月的所有文件并“执行任务”

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)  

I suggest use Series.str.contains : 我建议使用Series.str.contains

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

Or Series.str.startswith for filtering in pandas: Series.str.startswith用于过滤熊猫:

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

You can use glob ( https://docs.python.org/3/library/glob.html ) to use 2012-06-* pattern. 您可以使用glob( https://docs.python.org/3/library/glob.html )来使用2012-06-*模式。

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 使用此列表文件,您可以遍历并打开每个文件 ,例如

You can also query by year and month . 您还可以按yearmonth查询。

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)])

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

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