简体   繁体   English

在熊猫中将数据框分组的失败类型方法

[英]fail astype method to grouped dataframe in pandas

I get following error when I try to apply astype(float) method to grouped dataframe in pandas. 当我尝试将astype(float)方法应用于熊猫中的分组数据astype(float)时,出现以下错误。

ValueError: could not convert string to float:   

Do you know what is the reason why I cannot convert string to float by astype method? 您知道我无法通过astype方法将字符串转换为float的astype吗? and how I can solve this? 以及我该如何解决? Below is the code I get error and example data. 下面是我得到错误的代码和示例数据。

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  

Example data 示例数据

   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 

I believe you need to_numeric with parameter errors='coerce' for convert non numeric to NaN s: 我相信您需要to_numeric且参数errors='coerce'才能将非数字转换为NaN

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

If need remove all columns which are not parseable and no NaN s values in data is possible use boolean indexing and filter by notnull : 如果需要删除所有不可解析的列,并且数据中没有NaN值,则可以使用boolean indexing并通过notnull过滤:

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

In last version of pandas, 0.21.0 is possible use Series.notna : 在大熊猫的最后一个版本, 0.21.0可以使用Series.notna

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

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

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