Home > Enterprise >  Input all row values of dataframe into a variable
Input all row values of dataframe into a variable

Time:12-17

is there a way to get all output values of rows in dataframe into a variable?

my code is like this..

q1 = ps.sqldf("SELECT sdetframe.bill_no AS 'bill_number', sdetframe.ref_no AS 'ref_number', sdetframe.price_paid AS 'price_paid', sdetframe.raw_price AS 'raw_price', sdetframe.quanty AS 'quantity' FROM sdetframe WHERE ord_date = '"   dfd   "'")
q1df = DataFrame(q1)
q1data = q1df.at[0, 'bill_number']
print(q1df)

the output of q1df is this...

        bill_number  ref_number  price_paid  raw_price  quantity
0          197968           2      383.93     383.93       1.0
1          197968           2      383.93     383.93       1.0
2          197968           2      383.93     383.93       1.0
3          197969           1      357.14     357.14       1.0
4          197969         151      500.00     500.00       1.0

and the output of q1data is only 197968. is there a way where i can get all the bill number values and put it into a variable??

thanks in advance.

i put it into a variable so i can call it to another dataframe query. below is another code...

q2 = ps.sqldf("SELECT slsframe.pay_type AS 'pay_type', slsframe.received AS 'received' FROM slsframe WHERE bill_no = '"   str(q1data)   "'")
q2df = DataFrame(q2)

CodePudding user response:

If you want the variable to be a Series:

S1 = q1df['bill_number']

Output:

0    197968
1    197968
2    197968
3    197969
4    197969

If you want the variable to be a List:

L1 = q1df['bill_number'].tolist()

Output:

['197968', '197968', '197968', '197969', '197969']

CodePudding user response:

One way would be put all the values as a list using tolist().

q1data= q1df.bill_number.tolist()

Prints:

[197968, 197968, 197968, 197969, 197969]

EDIT User wants to use it for another SQL query as per question update.

For that, convert the list to tuple and call the query.

q1data = tuple(q1data)
ps.sqldf("SELECT slsframe.pay_type AS 'pay_type', slsframe.received AS 'received' FROM slsframe WHERE bill_no IN %(q1data)")

If you want duplicates removed from list then convert into set and then into tuple.

  • Related