Python Forum
Custom Function to Return Database Values
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Custom Function to Return Database Values
#1
A bit new and doing self teaching for python, but I am trying to figure out how best to use an existing dataframe to pass a column value into a where clause for a sql query to return results. However, it is not working as I am intending as it repeats the header from the custom function I am wrote to perform this. I would add that my goal is to solve how I can pass the dataframes column whether 10 or 10,000 records to get a value back from the database so I believe tuples will not work here.

Here is my function:
def overrides(id):
    sql = f"select name from sales..rep where id in({id})"
    mydf = pd.read_sql(sql, cnxn)
    return mydf
Here is how I apply it:
dfnew = dfrep['ID'].apply(overrides)
Here is the results
Output:
name 0 BOB name 0 JIM name 0 KEN name 0 GREG name 0 STEVE
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 571 Nov-23-2023, 02:53 PM
Last Post: rob101
  Need to return 2 values from 1 DF that equals another DF cubangt 5 637 Oct-21-2023, 02:45 PM
Last Post: deanhystad
  nested function return MHGhonaim 2 608 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,276 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  function return boolean based on GPIO pin reading caslor 2 1,170 Feb-04-2023, 12:30 PM
Last Post: caslor
  Adding values with reduce() function from the list of tuples kinimod 10 2,631 Jan-24-2023, 08:22 AM
Last Post: perfringo
  [Solved]Return values from npyscreen Extra 2 1,146 Oct-09-2022, 07:19 PM
Last Post: Extra
  Parallelism with return values Plexian 7 1,487 Aug-14-2022, 09:33 AM
Last Post: Plexian
  function accepts infinite parameters and returns a graph with those values edencthompson 0 853 Jun-10-2022, 03:42 PM
Last Post: edencthompson
  please help with classes and return values jamie_01 5 1,790 Jan-17-2022, 02:11 AM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020