2017-12-06 11 views
1

パンダのグループ化されたデータフレームにastype(float)メソッドを適用しようとすると、次のエラーが発生します。パンダのグループ化されたデータフレームへのastypeメソッドの失敗

ValueError: could not convert string to float: 

あなたは、私がastype方法で浮かぶように文字列を変換することができない理由が何であるかを知っていますか?どのように私はこれを解決することができますか? 以下はエラーとサンプルデータを取得するコードです。解析可能となしていないすべての列を削除する必要がある場合は

groupdf['Azimuth [deg]']= pd.to_numeric(groupdf['Azimuth [deg]'], errors='coerce') 

:私はあなたがNaN秒に変換数値以外のパラメータerrors='coerce'to_numericが必要と考えてい

def group(self,agg_method): 
    df=self.df 

    grouped=df.groupby(['Tilt [deg]', 'Azimuth [deg]'],as_index=False) 
    groupdf=grouped.agg(agg_method) 
    print(groupdf['Azimuth [deg]'][0],len(groupdf['Azimuth [deg]'][0])) 
    groupdf['Azimuth [deg]']=groupdf['Azimuth [deg]'].astype(float) <- I get error here 

例データ

Tilt [deg] Azimuth [deg] Glass SHGC Area of Multiplied Openings [m2] \ 
0  90.0  124.48  0.57       1450.24 
1  90.0   207.3  0.57       115.66 
2  90.0  207.47  0.57       115.62 
3  90.0  208.25  0.57        23.18 
4  90.0  208.26  0.57       113.12 
5  90.0  214.48  0.57       451.94 
6  90.0  218.57  0.57       230.08 
7  90.0  304.46  0.57        72.66 
8  90.0  304.48  0.57       1827.53 
9  90.0   34.48  0.57       917.29 

答えて

2

NaNのデータ値はboolean indexingで、フィルタはnotnull

パンダの最後のバージョンで
groupdf = groupdf[pd.to_numeric(groupdf['Azimuth [deg]'], errors='coerce').notnull()] 

0.21.0がある可能使用Series.notna

groupdf = groupdf[pd.to_numeric(groupdf['Azimuth [deg]'], errors='coerce').notna()] 
関連する問題