0
SELECT * FROM Sales where SKUDESC='samsung' AND Full_month='January'

i want output which this query gives but in python code

i am trying to groupby and i am getting name of groups .how to get values like it gives in SQL query

field=['product', 'month']
grouped=df.groupby(field).groups
3
  • show the python details code Commented Aug 1, 2019 at 7:41
  • There is no groupby in your sql query, why would you use groupby in youpython code? Commented Aug 1, 2019 at 7:46
  • i dont want to groupby i was trying groupby function to get same output as the query but its not working new_df = df.loc[(df.SKUDESC == 'samsung') & (df.Full_month == 'January')] this is working Commented Aug 1, 2019 at 8:38

2 Answers 2

1

SQL:

SELECT * FROM Sales where SKUDESC='samsung' AND Full_month='January'

Python:

new_df = df.loc[(df.SKUDESC == 'samsung') & (df.Full_month == 'January')]
Sign up to request clarification or add additional context in comments.

7 Comments

thanks it works .now i am trying to pass the variable in which samsung and january are stored i did this for sql query and it wored query="SELECT * FROM"+" "+ tablename+" "+ "where"+" " + field[0]+"="+"'"+values[0]+"'" +" "+ "AND" + " " +field[1]+"="+ "'"+values[1] query=query+ "'" print(query) but when i am doing it in this code its not working i want to pass the variable new_dfq = "(df."+field[0]+" == "+"'"+values[0]+"'"+ ") & (df." + field[1]+ " =="+"'"+ values[1]+"'"+")" print(new_dfq)
Sorry, but it is unclear and difficult to comprehend. Perhaps you can post another question in a clear manner so that someone can help you.
Also, if I understand you are trying to dynamically get the column name, I guess it should be something like df[(field[0]] == values[0]) & (field[1]] == values[1])]. Please try it.
okay forget it, the simple queston is that can i pass the variable in this df.loc function in which samsung and january are stored. i am trying but its not working
yes i am trying to dynamically get the column name and this is not working some key error
|
0

You can import the output of query in python dataframe like below.

df_data = pd.DataFrame(query ,columns=[‘col_1’, ‘col_2’])

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.