python - 用列中的值填充数据框中的所有 nan 值
问题描述
我有一个这样的df
index a b c
0 0 0 1
1 nan 1 2
2 0 1 3
3 1 nan 4
4 1 0 5
5 nan 0 6
6 nan nan 7
我想用最后一列的值填充前两列(实际上是前 20 列),即
index a b c
0 0 0 1
1 2 1 2
2 0 1 3
3 1 4 4
4 1 0 5
5 6 0 6
6 7 7 7
我正在考虑使用 df.iloc[:,0:1].fillna(df['c']) 之类的东西,但是它仅在我选择一列时才有效,如果选择了多个列,则所有内容都将保持为 nan。我想使用 iloc (或使用索引而不是列名)使用最后一列的值来填充前 20ish 列。
解决方案
做就是了
out = df.fillna(dict.fromkeys(list(df),df.c))
Out[206]:
a b c
0 0.0 0.0 1
1 2.0 1.0 2
2 0.0 1.0 3
3 1.0 4.0 4
4 1.0 0.0 5
5 6.0 0.0 6
6 7.0 7.0 7
推荐阅读
- scala - 从两个 Hive 表的连接创建一个结构数据类型数组
- cmake - CMake 使用与 CMakeLists.txt 不同的文件
- node.js - Nodejs 突然返回 undefined 到 require('http')';
- c++ - C++:模板特化,std map 选择了错误的特化
- javascript - Mirage 的简单 Ember 数据问题(错误:遇到未定义类型的资源对象)
- asp.net - 在 ASP Web API 中登录并注册 Xamarin 应用程序
- c# - 强制类具有类型参数的构造函数
- node.js - fs watch 在单次更改上触发两次
- debian - 如何在 Debian 8.11 上安装 clang-format?
- swift - 在 macOS 中以编程方式播放 Apple Keynote v8.2 演示文稿