首页 > 解决方案 > 我无法遍历数据框,因为该列被读取为字符串

问题描述

这是我的示例数据框和我试图通过它的循环。我还附上了我得到的错误。

示例数据框:

 x   Replicate  y
 1      1       0.39   
 2      2       0.08
 3      3       1.46

我的代码:

df = file[['x', 'Replicate', 'y']].copy()
df.columns = ['x', 'Replicate', 'y']
grouped = df.groupby(['Obs', 'Replicate']).mean()

######################## functions ##########################
def model(t, a, b, c):
 return (a-b)* np.exp(-c*t) + b

####################### curve fitting #######################
for g in grouped:
 print(type(g))
 # Specify initial guess
 a = g['y'].max()
 b = g['y'].min()
 c = 0.36

错误信息:a = g['y'].max() TypeError: string indices must be integers

标签: pythonpandasstringloops

解决方案


推荐阅读