- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个数据框 t_unit
,它是 pd.read_csv()
函数的结果。
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
我正在对数据帧重新采样以使用代码计算第 5 个和第 05 个百分位数:
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)
一切正常,但是当我通过 pd.concat
向我的数据框添加一列时,进入:
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'
你知道为什么吗?
最佳答案
有一些列不是数字的问题。你可以查看dtypes
:
print (t_unit.dtypes)
B18_LR_T float64
B18_B1_T float64
ext_T object
dtype: object
然后尝试先通过 astype
转换为数字:
t_unit.ext_T = t_unit.ext_T.astype(float)
如果:
ValueError: could not convert string to float
然后使用 to_numeric
使用参数 errors='coerce'
将错误数据转换为 NaN
:
t_unit.ext_T = pd.to_numeric(t_unit.ext_T, errors='coerce')
所有代码:
#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
关于python - 类型错误 : can't multiply sequence by non-int of type 'float' (python 2. 7),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39708133/
我正在使用 gmock 并模拟了一个函数 boost::beast::http::response_parser作为输出参数。功能签名看起来像: error_code readFromSocket(b
我的任务是打印由“非元音、元音、非元音”组成的单词列表,即 bab、bac、bad、bad ... 到 zuz。 我已经设法创建了一个代码,它执行前两个字母,但在最后一个循环中丢失并只打印'}' -
我正在尝试使用 label2rgb 生成 RGB 标签切片并使用它来更新 RGB 体积,如下所示: labelRGB_slice=label2rgb(handles.label(:,:,han
我有一个很奇怪的问题。我在 dll 中定义了一个接口(interface),如下所示: public interface IKreator2 { string Name { get; set;
在我的 openshift Drupal 托管中,网络都在 SSL 下 http://domain.com -> https://www.domain.com 确定 http://www.domain
我收到警告“退出构造函数时不可为空的事件‘SomeEvent’必须包含非空值。考虑将事件声明为可空。” 这是我的代码的一个非常简化的版本,它复制了完全相同的问题。我在这里错过了什么?这与 .Net 6
在一次大学考试中,我被要求测试一些 apache 簿记员类/方法,在这样做的过程中,我想在我的参数化测试中使用 mockito。没有 mockito 的测试工作正常但是当我尝试模拟接口(interfa
假设 A 列在 7 行中有以下值: 2 [空白的] 0 -0.3 0 [空白的] 0 如何获取范围(7 行)中非空/空白且不为零的最后一个值?因此,在这种情况下,正确答案是 -0.3。 最佳答案 =I
考虑以下受 this talk 启发的代码: template struct even_common_type_helper_impl; template struct even_common_typ
考虑这段代码, struct A {}; struct B { B(const A&) {} }; void f(B) { cout << "f()"<
考虑下面的类(class)。如果我对它运行 Findbugs,它会在第 5 行但不在第 7 行给我一个错误(“可序列化类中的非 transient 非可序列化实例字段”)。 1 public clas
我正在编写一个 python 脚本来计算 数据包丢失 通过使用 ping IP 地址linux 中的 subprocess 模块。 CSV 文件中保存了多个 IP 地址。当只给出可 ping 目的地时
我只是做文本更改,在文本之前它工作正常。请任何人都可以帮助我。 提前致谢 最佳答案 我已经解决了: ionic cordova 插件rmcordova-plugin-ionic-webview ion
我如何定义 在 persistence.xml 中? 我的项目在 Tomcat 6 和 Tomcat 7 中运行良好。 现在我正在使用 Struts 2 Spring 3.0.5 JPA 2 Jbos
我有一个 maven 仓库中不存在的第三方 jar,我们称它为“a.jar”,它也依赖于至少 20 多个第三方 jar,其中大部分不在 maven 中或者,我们称它们为“b.jar、c.jar、d.j
我已经浏览了各种线程很多小时(不夸张),但一直无法找到一种解决方案组合,使我能够将非 www 和 http 转发到 www 和 https,同时仍然能够查看 php 文件没有扩展名。如下是我的ngin
Scott Meyer 关于非成员函数增加封装并允许更优雅的设计(设计方面)的论点对我来说似乎非常有效。看这里:Article 但是我对此有疑问。 (似乎还有其他人,尤其是库开发人员,他们通常完全忽略
在对类设计的一些事实感到困惑时,特别是函数是否应该是成员,我查看了 Effective c++ 并找到了第 23 条,即 Prefer non-member non-friend functions
我正在尝试使用 firebase 云功能将通知发送到一个点半径的圆内的设备。我能够获取圈内设备的 ID,但无法获取 token ,使用 console.log(token) 打印时 token 为空。
我在我的项目中使用 React-ckeditor 5 包。我得到一个反序列化的 html 数据,我正在使用 React-html-parser 包将它解析成 html 模板,并将这个解析的数据传递给
我是一名优秀的程序员,十分优秀!