我正在从数据帧中分离出一个子集,并尝试将标头转换为值。这是我正在使用的子集
我正在尝试将标题转换回数据,并删除 mV 标签,但是当我将标题转换回一行时,Pandas 不允许我对其进行迭代。如何删除“mV_”文本并将值转换为浮点数?这是我迄今为止尝试过的。
def scatterer(df):
df=df.reset_index(drop=True)
df=df.drop(['Wavelength'], axis=1)
df = df.columns.to_frame().T.append(df, ignore_index=True)
df.columns = range(len(df.columns))
print(df.head(1))
for i in df.head(1):
i=i.replace("mV_", "")
i=float(i)]
这给出了错误
<块引用>"AttributeError: 'int' 对象没有属性 'replace'"
答案 0 :(得分:0)
这是您的解决方案:-
在 function
之外编写此代码:-
df=df.reset_index(drop=True)
df=df.drop(['Wavelength'], axis=1)
df = df.columns.to_frame().T.append(df, ignore_index=True)
df.columns = range(len(df.columns))
现在只需使用 apply()
方法:-
df.loc[0]=df.loc[0].apply(lambda x:x.replace('mV_',''))
现在只需使用 astype()
方法将它们转换为 float
df.loc[0].astype(float)
现在如果你打印 df
你会得到你想要的输出