Home > other >  How do I assign values from a list to another list of strings
How do I assign values from a list to another list of strings

Time:01-13

I am pretty new to Python so a few problems occurred I have an Excel Sheet with different entries and my goal is it to read each entry and automatically assign it to its name. By now this is a simplified sheet and more values could be added so i did not wanted to address each value after another.

So far i did this

import pandas as pd
import numpy as np

df = pd.read_excel('koef.xlsx')
data_array = np.array(df)

XCoeff = []
YCoeff = []
NCoeff = []
VarName = []

for i in range(len(data_array)):
    XCoeff.append(data_array[i][1])
XCoeff.pop(0)

for i in range(len(data_array)):
    YCoeff.append(data_array[i][2])
YCoeff.pop(0)

for i in range(len(data_array)):
    NCoeff.append(data_array[i][3])
NCoeff.pop(0)

for i in range(len(data_array)):
    VarName.append(data_array[i][0])
VarName.pop(0)

s1 = "X"
s2 = "Y"
s3 = "N"

XName = [s1   x for x in VarName]
YName = [s2   x for x in VarName]
NName = [s3   x for x in VarName]

In the end i want a list of Variables for X,Y and N where for example the first entries of X would be: Xdel = 0.00, Xdel2 = 4.44, Xdel3 = -2.06 and so on. With these variables i need to do calculations.

The Excel Sheet:

   Motion      X     Y     N
0    zero   0.00     0  0.00
1     del   0.00  4.44 -2.06
2    del2  -2.09 -0.24  0.16
3    del3   0.00 -2.95  1.38
4       u  -2.20     0  0.00
5      uu   1.50     X  0.00
6     uuu   0.00     0  0.00
7    udot  -1.47     0  0.00
8       v   0.11 -24.1 -7.94
9      vv   2.74  2.23 -1.15
10    vvv   0.00 -74.7  2.79
11   vdot   0.00 -16.4 -0.47
12      r  -0.07  4.24 -3.32
13     rr   0.58  0.56 -0.27
14    rrr   0.00  2.58 -1.25
15   rdot   0.00 -0.46 -0.75
16     vr  13.10     0  0.00
17    vrr   0.00 -40.3  8.08
18    vvr   0.00  -9.9 -3.37
19   udel   0.00 -4.56  2.32
20  vdel2   0.00  5.15 -1.17
21  vvdel   0.00   7.4 -3.41
22  rdel2   0.00 -0.51 -0.58
23  rrdel   0.00 -0.98  0.43

I hope the problem is stated clear, if not feel free to ask. Thank You

So far i got the lists at least working but i struggle to merge them.

CodePudding user response:

If you load the Excel sheet in a certain manner, accessing the cells by name can be simple. I think this will give you the cell access you need:

import pandas as pd

# Read the Excel sheet, index by the Motion column.
df = pd.read_excel('koef.xlsx', index_col='Motion')
print(df)
print(df.Y.del3)  # specific cells
print(df.N.vvr)

Output:

            X     Y     N
Motion                   
zero     0.00     0  0.00
del      0.00  4.44 -2.06
del2    -2.09 -0.24  0.16
del3     0.00 -2.95  1.38
u       -2.20     0  0.00
uu       1.50     X  0.00
uuu      0.00     0  0.00
udot    -1.47     0  0.00
v        0.11 -24.1 -7.94
vv       2.74  2.23 -1.15
vvv      0.00 -74.7  2.79
vdot     0.00 -16.4 -0.47
r       -0.07  4.24 -3.32
rr       0.58  0.56 -0.27
rrr      0.00  2.58 -1.25
rdot     0.00 -0.46 -0.75
vr      13.10     0  0.00
vrr      0.00 -40.3  8.08
vvr      0.00  -9.9 -3.37
udel     0.00 -4.56  2.32
vdel2    0.00  5.15 -1.17
vvdel    0.00   7.4 -3.41
rdel2    0.00 -0.51 -0.58
rrdel    0.00 -0.98  0.43
-2.95
-3.37

Caveat: The column/row names need to also be valid Python identifiers, but if not you can use for example df['Y']['del3'] syntax as well. Using valid Python identifiers makes the syntax easier to type.

  • Related