转换为float时列错误

问题描述 投票:1回答:1
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中的一些数据:

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]
A2425WC;Cyt;2425;2,78;0,23;12,14;-24,43;4,85;4,88;0,0005;0,0772;0,0646;0,3588;13,8400
C2489WCG;Cyt_Gen;2489;2,18;0,17;12,50;-24,51;5,53;4,21;0,0010;0,0639;0,0286;0,0601;0,6800
C2591WCG;Cyt_Gen;2591;5,13;0,36;14,29;-25,52;3,41;4,30;0,0046;0,0854;0,1169;0,7753;5,7000
E2695WC;Cyt;2695;1,43;0,14;10,55;-25,71;5,75;4,95;ND;0,0766;0,0441;0,0978;8,8500
A2900WC;Cyt;2900;6,95;0,53;13,11;-25,54;3,38;5,35;0,0032;0,1119;0,2356;1,8050;14,7100
A2800WC;Cyt;2800;1,88;0,16;11,62;-23,68;5,88;5,28;0,0025;0,0983;0,0770;0,3777;5,4200
A3050WC;Cyt;3050;1,50;0,12;12,50;-24,62;2,23;5,97;ND;0,0696;0,0729;0,5736;9,4000

当我运行它时,我收到错误:

ValueError: could not convert string to float: 'Elevation'

好像它正试图转换标题,但我只想转换列表。

python python-3.x list pandas
1个回答
1
投票

要将列转换为数字,您应该使用pd.to_numeric

cols = ['Elevation','C','N','C/N','Soil13C','Soil15N','pH','Na_[mg/g]','K_[mg/g]','Mg_[mg/g]','Ca_[mg/g]','P_[µg/g]']

df[cols] = df[cols].apply(pd.to_numeric, errors='coerce')

您的代码将无法工作,因为像字典及其键一样,当您遍历数据框时,您只能找到列标题。

更新1

还可以尝试使用以下选项:

import pandas as pd
from io import StringIO

mystr = StringIO(""" <bound method NDFrame.head of ID VegType Elevation C N C/N Soil13C Soil15N pH \ 
1 C2489WCG Cyt_Gen 2489 2,18 0,17 12,50 -24,51 5,53 4,21
2 C2591WCG Cyt_Gen 2591 5,13 0,36 14,29 -25,52 3,41 4,30
3 E2695WC Cyt 2695 1,43 0,14 10,55 -25,71 5,75 4,95
""")

df = pd.read_csv(mystr, skiprows=1, decimal=',', header=None, delim_whitespace=True)

#    0         1        2     3     4     5      6      7     8     9
# 0  1  C2489WCG  Cyt_Gen  2489  2.18  0.17  12.50 -24.51  5.53  4.21
# 1  2  C2591WCG  Cyt_Gen  2591  5.13  0.36  14.29 -25.52  3.41  4.30
# 2  3   E2695WC      Cyt  2695  1.43  0.14  10.55 -25.71  5.75  4.95

更新2

import pandas as pd
from io import StringIO

mystr = StringIO("""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]
A2425WC;Cyt;2425;2,78;0,23;12,14;-24,43;4,85;4,88;0,0005;0,0772;0,0646;0,3588;13,8400
C2489WCG;Cyt_Gen;2489;2,18;0,17;12,50;-24,51;5,53;4,21;0,0010;0,0639;0,0286;0,0601;0,6800
C2591WCG;Cyt_Gen;2591;5,13;0,36;14,29;-25,52;3,41;4,30;0,0046;0,0854;0,1169;0,7753;5,7000
E2695WC;Cyt;2695;1,43;0,14;10,55;-25,71;5,75;4,95;ND;0,0766;0,0441;0,0978;8,8500
A2900WC;Cyt;2900;6,95;0,53;13,11;-25,54;3,38;5,35;0,0032;0,1119;0,2356;1,8050;14,7100
A2800WC;Cyt;2800;1,88;0,16;11,62;-23,68;5,88;5,28;0,0025;0,0983;0,0770;0,3777;5,4200
A3050WC;Cyt;3050;1,50;0,12;12,50;-24,62;2,23;5,97;ND;0,0696;0,0729;0,5736;9,4000
""")

df = pd.read_csv(mystr, decimal=',', delimiter=';')

#          ID  VegType  Elevation     C     N    C/N  Soil13C  Soil15N    pH  \
# 0   A2425WC      Cyt       2425  2.78  0.23  12.14   -24.43     4.85  4.88   
# 1  C2489WCG  Cyt_Gen       2489  2.18  0.17  12.50   -24.51     5.53  4.21   
# 2  C2591WCG  Cyt_Gen       2591  5.13  0.36  14.29   -25.52     3.41  4.30   
# 3   E2695WC      Cyt       2695  1.43  0.14  10.55   -25.71     5.75  4.95   
# 4   A2900WC      Cyt       2900  6.95  0.53  13.11   -25.54     3.38  5.35   
# 5   A2800WC      Cyt       2800  1.88  0.16  11.62   -23.68     5.88  5.28   
# 6   A3050WC      Cyt       3050  1.50  0.12  12.50   -24.62     2.23  5.97   

#   Na_[mg/g]  K_[mg/g]  Mg_[mg/g]  Ca_[mg/g]  P_[µg/g]  
# 0    0,0005    0.0772     0.0646     0.3588     13.84  
# 1    0,0010    0.0639     0.0286     0.0601      0.68  
# 2    0,0046    0.0854     0.1169     0.7753      5.70  
# 3        ND    0.0766     0.0441     0.0978      8.85  
# 4    0,0032    0.1119     0.2356     1.8050     14.71  
# 5    0,0025    0.0983     0.0770     0.3777      5.42  
# 6        ND    0.0696     0.0729     0.5736      9.40 
© www.soinside.com 2019 - 2024. All rights reserved.