簡體   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