0

Say I have a dataframe df which looks like the below:

enter image description here

I can query the dataframe using pandas .query to return specific rows as below:

df.query('Data == "05h"')

Is it possible to amend the above to return rows without hard coding the 0 before 5h, so it essentially returns all rows which contains 5h in the string?

Any help appreciated.

K.Best
  • 79
  • 8
  • Does this answer your question? [Pandas - equivalent of str.contains() in pandas query](https://stackoverflow.com/questions/38662011/pandas-equivalent-of-str-contains-in-pandas-query) – G. Anderson Mar 14 '23 at 15:31

3 Answers3

1

You can search for the substring you want as follows

df2 = df[df['Data'].str.contains('5h')]
1

Here is my proposition

import pandas as pd

df = pd.DataFrame({'Date': ['2023-01-01', '2023-02-01', '2023-02-01', '2023-02-01'],
                   'data': ['05h', '05f', '05h', '05f']})

df = df.loc[(df['data'].str.contains('5h'))]
Paulina
  • 159
  • 10
1

Yes, you can do it this way:

df.loc[df.Data.str.contains('5h')]

Also, you could get the same result as your query using:

df.loc[df.Data == '05h']

this way you don't have to hardcode your column name.

R_D
  • 430
  • 1
  • 7