将int列乘以float常量 pandas 数据框 [英] Multiply int column by float constant pandas dataframe

查看:176
本文介绍了将int列乘以float常量 pandas 数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆小费斗降雨数据记录为每分钟间隔的小费数量.我已将其上载到pandas数据框,并且我需要能够将每个单独的列乘以mm/tip校准系数,但是该列的类型为int且该因子的类型为float.我尝试过:

I have a bunch of tipping bucket rainfall data recorded as number of tips per each minute interval. I have uploaded this to a pandas dataframe and I need to be able to multiply each individual column by a mm/tip calibration factor, but the column is type int and the factor is type float. I've tried:

df ['Series'] = df ['Series'].mul(constant)-> TypeError:*:'NoneType'和'float'不受支持的操作数类型

df['Series'] = df['Series'].mul(constant) -> TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'

df ['Series'] * =常量-> TypeError:无法将序列乘以'float'类型的非整数

df['Series'] *= constant -> TypeError: can't multiply sequence by non-int of type 'float'

df ['Series'] = df ['Series'].astype(float)*常量-> ValueError:无法将字符串转换为float:

df['Series'] = df['Series'].astype(float) * constant -> ValueError: could not convert string to float:

必须有一种简单的方法来做到这一点...帮助?

There's got to be an easy way to do this... Help?

这是我的数据:

这是我的阅读方式:

    def loaddata(filepaths):
        t1 = time.clock()
        for i,filepath in enumerate(filepaths):
            xl = pd.ExcelFile(filepath)
            df = xl.parse(xl.sheet_names[0], header=0, index_col=2, skiprows=[0,2,3,4], parse_dates=True)
            df = df.dropna(axis=1, how='all') 
            df = df.drop(['Decimal Year Day', 'Decimal Year Day.1', 'RECORD'], axis=1)
            df.index = pd.DatetimeIndex(((df.index.asi8/(1e9*60)).round()*1e9*60).astype(np.int64)).values

    return df

files = ["London Water Balance.xlsx"]
Water = loaddata(files)

在这里dtype

Water.dtypes

[L] Drainage NE             float64
[L] Drainage SE              object
[L] Embedded raingauge E     object
[L] External raingauge       object
dtype: object

推荐答案

尝试:

df.convert_objects(convert_numeric=True)

将其强制为数字列,并将非数字元素设置为nan.

Will force it to a numeric column and set non-numeric elements to nan.

这篇关于将int列乘以float常量 pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆