繁体   English   中英

在熊猫数据帧切片上相乘

[英]Multiplication on slice of pandas dataframe

我有一个这样的数据框:

  Year       A_annex    Arnston     Bachelor         Berg   
  1955      1.625       0.940         NaN            NaN   
  1956      1.219       1.018         NaN            NaN   
  1957      2.090       1.20          NaN            1.190   
  1958      0.950       1.345         NaN            1.090

我想将[1:,1:]的所有内容乘以.404

我正在尝试的代码是:

df=pd.read_csv(r'H:\Sheyenne\Grazing Records\Master_AMU_complete.csv')
hectare=0.404
df=df.iloc[1:,1:]
df=df*hectare

但这返回:

TypeError: Could not operate 0.404686 with block values can't multiply sequence by non-int of type 'float'

打印df.info()表示切片后的所有内容都是非null对象(如果有帮助的话)。

是的,这是有问题的价值。 您可以按功能找到这些有问题的值(感谢ajcr):

df = df.convert_objects(convert_numeric=True) 

首先将NaN转换为0 ,然后在上方应用函数,然后返回NaN而不是有问题的值。 因此,您必须找到具有NaN值的行并返回原始df子集。

print df
   Year  A_annex Arnston  Bachelor  Berg
0  1955    1.625   0.940       NaN   NaN
1  1956    1.219   1.018       NaN   NaN
2  1957    2.090   1.20a       NaN  1.19
3  1958    0.950  1.345a       NaN  1.09

test = df.fillna(0)
test = test.convert_objects(convert_numeric=True)

   Year  A_annex  Arnston  Bachelor  Berg
0  1955    1.625    0.940         0  0.00
1  1956    1.219    1.018         0  0.00
2  1957    2.090      NaN         0  1.19
3  1958    0.950      NaN         0  1.09

test = df[test.isnull().any(axis=1)]

   Year  A_annex Arnston  Bachelor  Berg
2  1957     2.09   1.20a       NaN  1.19
3  1958     0.95  1.345a       NaN  1.09

hectare=0.404
df=df.iloc[1:,1:]
df=df*hectare
print df

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM