MAAHE
MAAHE

Reputation: 169

For loop with zip

I am looking for some help, I have this example

numbers=['Sub1','Sub2','Sub3']
Value=[60,45,30]
Y = pd.Series()
for number,i in zip(numbers,Value):
    Y[number]=math.cos(i)
    print(Y)

I get this result

Sub1   -0.952413
dtype: float64
Sub1   -0.952413
Sub2    0.525322
dtype: float64
Sub1   -0.952413
Sub2    0.525322
Sub3    0.154251
dtype: float64

but what I hope to get is

Sub1   -0.952413
Sub2    0.525322
Sub3    0.154251

or

-0.952413
 0.525322
 0.154251

because I will need those values to complete the code. Thanks BR Beginner

Upvotes: 1

Views: 90

Answers (1)

oppressionslayer
oppressionslayer

Reputation: 7224

Try this:

list(zip(numbers,[f'"{math.cos(i):.6f}' for i in Value]))                                                                                                                           
[('Sub1', '"-0.952413'), ('Sub2', '"0.525322'), ('Sub3', '"0.154251')]

or:

pd.DataFrame(list(zip(numbers,[f'{math.cos(i):.6f}' for i in Value])))


      0          1
0  Sub1  -0.952413
1  Sub2   0.525322
2  Sub3   0.154251

Upvotes: 1

Related Questions