Creating a list from series of pandas

70 Views Asked by At

Click here for the imageI m trying to create a list from 3 different series which will be of the shape "({A} {B} {C})" where A denotes the 1st element from series 1, B is for 1st element from series 2, C is for 1st element from series 3 and this way it should create a list containing 600 element.

List 1              List 2           List 3
u_p0 1              v_p0 2           w_p0 7
u_p1 21             v_p1 11          w_p1 45
u_p2 32             v_p2 25          w_p2 32
u_p3 45             v_p3 76          w_p3 49
...                 ....             ....
u_p599 56           v_p599 78        w_599 98

Now I want the output list as follows

    (1 2 7)
    (21 11 45)
    (32 25 32)
    (45 76 49)
.....

These are the 3 series I created from a dataframe

r1=turb_1.iloc[qw1] #List1
r2=turb_1.iloc[qw2] #List2
r3=turb_1.iloc[qw3] #List3

Pic of the seriesFor the output I think formatted string python method will be useful but I m quite not sure how to proceed.

turb_3= ["({A} {B} {C})".format(A=i,B=j,C=k) for i in r1 for j in r2 for k in r3] 

Any kind of help will be useful.

1

There are 1 best solutions below

0
On BEST ANSWER

Use pandas.DataFrame.itertuples with str.format:

# Sample data
print(df)
   col1  col2  col3
0     1     2     7
1    21    11    45
2    32    25    32
3    45    76    49

fmt = "({} {} {})"

[fmt.format(*tup) for tup in df[["col1", "col2", "col3"]].itertuples(False, None)]

Output:

['(1 2 7)', '(21 11 45)', '(32 25 32)', '(45 76 49)']