我正在做一個涉及兩個變數的回圈。我已經解決了第一個問題,但第二個問題是一個相當大的挑戰。
到目前為止,我所做的事情如下所示。
columns = ('node2'/span>, 'node3'/span>, 'node4'/span>, 'node5'/span>, 'node6'/span>, 'node7'/span>, 'node8'/span>, 'node9'/span>)
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)
節點
。
正如你所看到的,我能夠將列名從 "node1 "改成 "node9"
。然而,我還想改變我的代碼第四行中的變數。 這些變數分別是 L1_20、L2_20、L3_20 和 L4_20。
所以我試著加入這段代碼。
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]))
輸出:
所以我的代碼現在看起來像這樣:
columns = ('node2', 'node3', 'node4', 'node5'/span>, 'node6'/span>, 'node7'/span>, 'node8'/span>, 'node9'/span>)
加速度=(20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90)
x = 20 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)
節點
其中我得到一個錯誤說。 "string indices must be integers"
我做錯了什么?
uj5u.com熱心網友回復:
你正在建立一個字串,然后將其作為同名的變數進行索引,它與變數沒有關系,只是一個新的字串。為了建立一個變數的名字并使用它,你可以使用一個字典將字串映射到變數,并對字典進行索引以使用該變數。
我相信你是想把所有的組合串聯到一個Dataframe中,如果是這樣的話,你可以選擇每個獨立的Dataframe的所有列,并把它們一起追加到node:
mapping = {
'L1_25'。L1_25,
'L2_25': L2_25,
'L3_25': L3_25,
'L4_25': L4_25,
... # build for all combinations。
}
columns = ['node2'/span>, 'node3'/span>, 'node4'/span>, 'node5'/span>, 'node6'/span>, 'node7'/span>, 'node8'/span>, 'node9'/span>]
加速度 = [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}'][列], node], axis=1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/316461.html
標籤:



