df = pd.read_csv('TestdataNALA.csv', sep=';', na_values='ND')
df.fillna(0, inplace=True)
df = df[['ID','VegType','Elevation','C','N','C/N','Soil13C','Soil15N','pH','Na_[mg/g]','K_[mg/g]','Mg_[mg/g]','Ca_[mg/g]','P_[µg/g]']]
new_list = df[['Elevation','C','N','C/N','Soil13C','Soil15N','pH','Na_[mg/g]','K_[mg/g]','Mg_[mg/g]','Ca_[mg/g]','P_[µg/g]']]
nnew_list = [float(i) for i in new_list]
print(type(df['ID']))
#--> <class 'pandas.core.series.Series'>
print('ID' in df.columns)
#-->True
来自CSV的一些数据:
^{pr2}$当我运行这个时,我得到一个错误:
ValueError: could not convert string to float: 'Elevation'
它似乎在试图转换标题,但我只想转换列表。在
要将列转换为数字,应使用
pd.to_numeric
:您的代码将无法工作,因为与字典及其键一样,当您在数据帧中循环时,您只能找到列标题。在
更新1
请尝试使用以下选项:
^{pr2}$更新2
相关问题 更多 >
编程相关推荐