0

I am doing a loop where it involves two variables. I already have solved the first one but the second one is quite a challenge.

What I have done so far is shown below.

columns = ('node2', 'node3', 'node4', 'node5', 'node6', 'node7', 'node8', 'node9')
node = pd.concat([L1_20['node1'], L2_20['node1'], L3_20['node1'], L4_20['node1']],axis=1)
for i in columns:

   node = pd.concat([node, L1_20[f'{i}'], L2_20[f'{i}'], L3_20[f'{i}'], L4_20[f'{i}']],axis=1)
   node

enter image description here

As you can see, I was able to work with cahnging the column names from "node1" to "node9"

However, I also wanted to change my varialbes in the 4th line of my code. The variables are L1_20, L2_20, L3_20, and L4_20 respectively.

So I tried to incorporate this code.

acceleration = (20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90)
i = 20
for i in range(len(acceleration)):
print('L1_'+str(acceleration[i]))

Output:

enter image description here

So my code now looks like this:

columns = ('node2', 'node3', 'node4', 'node5', 'node6', 'node7', 'node8', 'node9')
acceleration = (20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90)
x = 20

node = pd.concat([L1_25['node1'], L2_25['node1'], L3_25['node1'], L4_25['node1']],axis=1)

for i in columns:
   for x in range(len(acceleration)):

   node = pd.concat([node, 'L1_'+str(acceleration[x])[f'{i}'], 'L2_'+str(acceleration[x])[f'{i}'], 'L3_'+str(acceleration[x])[f'{i}'], 'L4_'+str(acceleration[x])[f'{i}']],axis=1)
   node

which I got an error saying: "string indices must be integers"

What did I do wrong?

enter image description here

4
  • To do this you would have to use a data structure like a dictionary to map the name with the variable. That way you can do mapping['L1_'+str(acceleration[x])][f'{i}'], where mapping maps each string to the variable of the same name. Commented Sep 3, 2021 at 9:35
  • What did I do wrong? : you are building a string and not pointing to the variable of the same name. That way when you call <string>[f'{i}'] you are indexing the string. Python then complains that to index a string the data type in the square brackets must be an integer. Commented Sep 3, 2021 at 9:39
  • @ChrisOram can you show me an example of a dictionary to map the name with the variables? If you don't mind. Thank you a lot Commented Sep 3, 2021 at 9:45
  • I have added an answer with more explanation and an example for this use case. Commented Sep 3, 2021 at 10:07

1 Answer 1

1

You are building a string and then indexing it as if it was the variable of the same name, it has no relation to the variable it is just a new string. In order to build the name of a variable and use it you can map the string to the variable using a dictionary and index the dictionary to use the variable.

I believe you are trying to concatenate all of the combinations into one Dataframe, if this is the case you can select all columns for each seperate dataframe and append them to node together:

mapping = {
    'L1_25': L1_25,
    'L2_25': L2_25,
    'L3_25': L3_25,
    'L4_25': L4_25,
    ...             # build for all combinations
}

columns = ['node2', 'node3', 'node4', 'node5', 'node6', 'node7', 'node8', 'node9']
acceleration = [20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90]
node = pd.DataFrame()

for i in range(1, 5):
    for acc in acceleration:
        node = pd.concat([mapping[f'L{i}_{acc}'][columns], node], axis=1)
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for this one. I see it uses append. I have to concat my data though :)
Yep, I have amended to fit your data. Glad it helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.