问题描述
我有一个数据帧 t_unit,它是 pd.read_csv() 函数的结果.
日期时间 B18_LR_T B18_B1_T2016 年 3 月 24 日 09:00 21.274 21.17924/03/2016 10:00 19.987 19.86824/03/2016 11:00 21.632 21.41724/03/2016 12:00 26.285 24.7792016 年 3 月 24 日 13:00 26.897 24.779
我正在重新采样数据框以使用代码计算第 5 个和第 05 个百分位数:
keys_actual = list(t_unit.columns.values)对于keys_actual中的键:ts_wk = t_unit[key].resample('W-MON')ts_wk_05p = ts_wk.apply(lambda x: x.quantile(0.05)).round(decimals=1).rename(key+'_05p', inplace=True)ts_wk_95p = ts_wk.apply(lambda x: x.quantile(0.95)).round(decimals=1).rename(key+'_95p', inplace=True)
一切正常,但是当我通过 pd.concat 向我的数据框添加一列时,进入:
日期时间 B18_LR_T B18_B1_T ext_T2016 年 3 月 24 日 09:00 21.274 21.179 6.92016 年 3 月 24 日 10:00 19.987 19.868 7.52016 年 3 月 24 日 11:00 21.632 21.417 9.12016 年 3 月 24 日 12:00 26.285 24.779 9.92016 年 3 月 24 日 13:00 26.897 24.779 9.2
<块引用>
ts_wk_05p = ts_wk.apply(lambda x: x.quantile(0.05)).round(decimals=1).rename(key+'_05p', inplace=True)
TypeError: 不能将序列乘以float"类型的非整数
你知道为什么吗?
有些列不是数字的问题.您可以查看 dtypes一个>:
打印 (t_unit.dtypes)B18_LR_T 浮点数64B18_B1_T 浮点数64ext_T 对象数据类型:对象
然后尝试先通过 转换为数字astype:
t_unit.ext_T = t_unit.ext_T.astype(float)
如果:
<块引用>ValueError: 无法将字符串转换为浮点数
然后使用 to_numeric 带有参数errors='coerce' 用于将坏数据转换为NaN:
t_unit.ext_T = pd.to_numeric(t_unit.ext_T, errors='coerce')
所有代码:
#模拟字符串列t_unit.ext_T = t_unit.ext_T.astype(str)打印(t_unit.dtypes)B18_LR_T 浮点数64B18_B1_T 浮点数64ext_T 对象数据类型:对象#转换为浮动t_unit.ext_T = t_unit.ext_T.astype(float)打印(t_unit)L = []对于 t_unit.columns 中的键:ts_wk = t_unit[key].resample('W-MON')#remove 就地=真ts_wk_05p = ts_wk.apply(lambda x: x.quantile(0.05)).round(decimals=1).rename(key+'_05p')ts_wk_95p = ts_wk.apply(lambda x: x.quantile(0.95)).round(decimals=1).rename(key+'_95p')L.append(ts_wk_05p)L.append(ts_wk_95p)打印(pd.concat(L,轴=1))B18_LR_T_05p B18_LR_T_95p B18_B1_T_05p B18_B1_T_95p ext_T_05p 约会时间2016-03-28 20.2 26.8 20.1 24.8 7.0ext_T_95p约会时间2016-03-28 9.8
I have a dataframe t_unit, which is the result of a pd.read_csv() function.
datetime B18_LR_T B18_B1_T 24/03/2016 09:00 21.274 21.179 24/03/2016 10:00 19.987 19.868 24/03/2016 11:00 21.632 21.417 24/03/2016 12:00 26.285 24.779 24/03/2016 13:00 26.897 24.779
I am resampling the dataframe to calculate the 5th and 05th percentiles with the code:
keys_actual = list(t_unit.columns.values) for key in keys_actual: ts_wk = t_unit[key].resample('W-MON') ts_wk_05p = ts_wk.apply(lambda x: x.quantile(0.05)).round(decimals=1).rename(key+'_05p', inplace=True) ts_wk_95p = ts_wk.apply(lambda x: x.quantile(0.95)).round(decimals=1).rename(key+'_95p', inplace=True)
All works fine, but when I add a column to my dataframe, by means of pd.concat, into:
datetime B18_LR_T B18_B1_T ext_T 24/03/2016 09:00 21.274 21.179 6.9 24/03/2016 10:00 19.987 19.868 7.5 24/03/2016 11:00 21.632 21.417 9.1 24/03/2016 12:00 26.285 24.779 9.9 24/03/2016 13:00 26.897 24.779 9.2
ts_wk_05p = ts_wk.apply(lambda x: x.quantile(0.05)).round(decimals=1).rename(key+'_05p', inplace=True)
TypeError: can't multiply sequence by non-int of type 'float'
Do you have any idea why?
There is problem some column is not numeric. You can check dtypes:
print (t_unit.dtypes) B18_LR_T float64 B18_B1_T float64 ext_T object dtype: object
Then try convert to numeric first by astype:
t_unit.ext_T = t_unit.ext_T.astype(float)
If:
ValueError: could not convert string to float
then use to_numeric with parameter errors='coerce' for convert bad data to NaN:
t_unit.ext_T = pd.to_numeric(t_unit.ext_T, errors='coerce')
All code:
#simulate string column t_unit.ext_T = t_unit.ext_T.astype(str) print (t_unit.dtypes) B18_LR_T float64 B18_B1_T float64 ext_T object dtype: object #convert to float t_unit.ext_T = t_unit.ext_T.astype(float) print (t_unit) L = [] for key in t_unit.columns: ts_wk = t_unit[key].resample('W-MON') #remove inplace=True ts_wk_05p = ts_wk.apply(lambda x: x.quantile(0.05)).round(decimals=1).rename(key+'_05p') ts_wk_95p = ts_wk.apply(lambda x: x.quantile(0.95)).round(decimals=1).rename(key+'_95p') L.append(ts_wk_05p) L.append(ts_wk_95p) print (pd.concat(L, axis=1)) B18_LR_T_05p B18_LR_T_95p B18_B1_T_05p B18_B1_T_95p ext_T_05p datetime 2016-03-28 20.2 26.8 20.1 24.8 7.0 ext_T_95p datetime 2016-03-28 9.8