我试试这个for循环。
this payment of €" & [Forms]![frmClaimDataEntry]!
我的输入数据框:
import pandas as pd
import csv
import numpy as np
Input = pd.read_csv('C:/PyTemp/Input.csv')
Input = pd.DataFrame(Input)
print (Input)
AGE = Input['AGE']
QUALITY = Input['QUALITY']
for i in range(1,len(Input)):
Input['DELTA'] = Input['QUALITY'] * .1
Input.iloc[i]['QUALITY'] = Input.iloc[i-1]['QUALITY']+ Input.iloc['i']['DELTA']
print(Input)
我想要达到的结果:
QUALITY AGE
0 15 10
0 11
0 12
这是完整的错误:
QUALITY AGE DELTA
0 15 10 1.5
0 16.5 11 1.65
0 18.15 12 1.815
我也试过这个循环。最终值是正确的,但它会覆盖先前的值
TypeError: cannot do label indexing on <class 'pandas.core.index.Int64Index'> with these indexers [i] of <type 'str'>
结果:
import pandas as pd
import csv
import numpy as np
Input = pd.read_csv('C:/PyTemp/Input.csv')
Input = pd.DataFrame(Input)
print (Input)
AGE = Input['AGE']
QUALITY = Input['QUALITY']
for i in range(1,len(Input)):
Input['DELTA'] = Input['QUALITY'] * .1
Input['QUALITY'] = Input['QUALITY'][i-1]+Input['DELTA']
print(Input)
答案 0 :(得分:1)
for循环实际上并没有按照你的想法进行。这一行~
实际上是通过每个循环分配整个列。您可以通过在for循环之外执行此操作来实现该列的结果。您收到错误,因为Input['DELTA'] = Input['QUALITY'] * .1
只接受整数,而不是字符串。只要您的索引是有序且连续的({0,1,2 ......)
.iloc
将适用于您
.loc
请注意,我不一定认为这是实现结果的最佳方法,但是我试图解决有关使用for循环来实现所需结果的原始问题。