python - 遍历具有索引的列
问题描述
我如何在 pandas 中迭代具有索引的列,对于我们可以使用的行for i, j in df.iterrows():
,它将给出索引和行。
列有类似的东西吗?
spice smice skice bike dike mike
1 23 35 34 34 56
135 34 23 21 56 34
231 12 67 21 62 75
我正在尝试使用嵌套的 for 循环,如下所示:
for index, col1 in df.columns:
for col2 in df.columns[index:]:
有没有更好的方法来做到这一点?
解决方案
我相信您需要按列名循环并按列名Series
选择:
for col_name in df.columns:
print (col_name)
print (df[col_name])
替代解决方案,缺点是可读性更差:
for col_name in df:
print (col_name)
print (df[col_name])
您的解决方案可以使用 transpose by DataFrame.T
,但在我看来有点过于复杂:
for col_name, s in df.T.iterrows():
print (col_name)
print (s)
编辑:
for col_name in df.columns:
print (col_name)
print (df[col_name])
print (df.columns.get_loc(col_name))
推荐阅读
- azure-logic-apps - 存储在变量中的 json 的逻辑应用读取属性
- vb.net - 从字符串“”到类型“Double”的转换无效。
- matlab - 在配置 CMake 时选择使用 LibR 的 Matlab 包装器
- javascript - 在我的屏幕上绘制黑色画布不起作用,没有绘制黑色矩形
- apache - 如何仅使用 .htaccess 重定向我的主页
- vba - 如何在 Visio 绘图中查找所有形状并将每个形状添加到数组中?
- mysql - 如何正确地对具有条件的 UPDATE 列进行 SQL 请求?
- c# - 配对系统 N vs N 来自列表
- wxpython - 如何正确调整 wx.grid.Grid 的大小,使其不覆盖其他控件?
- apache-nifi - NiFi:使用 ExecuteScript 查询记录