pandas - 如何在没有嵌套 for 循环的情况下遍历数据框?
问题描述
如何在不嵌套 for 循环的情况下遍历 pandas 中的数据框?
我的代码是:
df = pd.DataFrame([[1,2,3],[4,5,6],[7,8,9]],columns=['a','b','c'])
解决方案
也许您想尝试使用此方法遍历每一行和每一列。
print([df.loc[row, col] for row in df.index for col in df.columns])
这输出:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
推荐阅读
- php - 删除php数组中的重复值
- java - 使用准备好的语句以 blob 格式将图像插入数据库
- ruby-on-rails - Dockerizing 现有 Rails 应用程序
- node.js - 在托管程序中将我的域重定向到我的 vps 服务器不起作用
- rust - 在 Rust 的 Cursive 库中更新 TextView 的正确方法
- android - Android/Kotlin 中的操作顺序
- javascript - javascript按目录读取多个文件
- c# - Unity 3d 碰撞检测
- android - 使用 Mapbox Android 中的循环从 geojson 添加许多符号图标?
- sockets - 游戏客户端如何从服务器接收 UDP 数据?