1

I'm sure what I'm asking is simple question but have yet to figure it out. I have a panda df and I want to run this basic query on it

Select a,b,c 
FROM TABLE
WHERE (TABLE.time >= x ) && (TABLE.time <= y)
GROUP BY c

so if I have a table

A    B    time

a    b    time1
c    d    time2
e    f    time3

I would want only to return the a,b,c where the time is greater or less than the ones in the query. Also would this query on a Dataframe give me another df if I assign the query to a variable say something like

df2 = df.query()

I hope this makes sense

3
  • So what's holding you back from implementing it? Commented Sep 19, 2018 at 19:42
  • you can actually use df.query in case you didn't know pandas.pydata.org/pandas-docs/version/0.22.0/generated/… Commented Sep 19, 2018 at 19:46
  • yes I've looked into df.query() but the first parameter of the function ask for a string but im not sure of the syntax here since its not the regular SQL query. Im pretty new to pandas so im not sure how to do this statement Commented Sep 19, 2018 at 19:55

2 Answers 2

1

As mentioned in Documents docs

The query() method uses a slightly modified Python syntax by default. It is used to apply condition like greater then less then. query method does not support group by itself instead data frame have method groupby which works the same way.

I attempted to write code for your query take a look at it :

g= table.query('time>=x and time<y').groupby('C')

for name,group in g:
    print(name , group[['a','b','c']])

Without using query() :

g = table[(table.time>=x) & (table.time <= y)].groupby('C')

for name,group in g:
        print(name , group[['a','b','c']])
Sign up to request clarification or add additional context in comments.

Comments

0

So i'm sure this isn't the best work-around but it worked for me.

df = pd.read_excel("file.xlsx", index_col= None, na_values=['NA'] , usecols=[18,4,5,21,0,1])
df2 = df[(df.TIME >= x) , (df.TIME <= y)]
df3 = df2[['a','b','c']]

That help me get the a,b,c within the time range I put

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.