python - Pandas如何获取行的索引?

标签 python pandas indexing

enter image description here

import pandas as pd
data = pd.read_csv('traj2_Binarization.csv', sep=",", index_col="NAME")

for index, row in data.iterrows():
    print(row)
    -----results------
    RUNX3            0
    ROGDI            0
    FSTL3            0
    MOCOS            0
    PDCD7            1
    MYO15A           0
    MYO9B            0
    MAP1LC3A         0
    TBC1D19          0
    ASIC1            0
    RAP1A            0
    ARAP3            0
    IQSEC2           0
    HIVEP3           0

Here, how can I make the results converted to below?

RUNX3 = False
ROGDI = False
FSTL3 = False
MOCOS = False
PDCD7 = True
.
.
.

最佳答案

除了 @jezrael 的回答之外,如果版本低于 3.6:

for index, row in data.iterrows():
    print('%s = %s'%(index,bool(row['whatever is the column'])))

或者:

for index, row in data.iterrows():
    print('{0} = {1}'.format(index,bool(row['whatever is the column'])))

或者:

for index, row in data.iterrows():
    print(index,'=',bool(row['whatever is the column']))

所有输出:

RUNX3 = False
ROGDI = False
FSTL3 = False
MOCOS = False
PDCD7 = True
MYO15A = False
MYO9B = False
MAP1LC3A = False
TBC1D19 = False
ASIC1 = False
RAP1A = False
ARAP3 = False
IQSEC2 = False
HIVEP3 = False

关于python - Pandas如何获取行的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53184739/

相关文章:

python - 构建标签云的巧妙方法? - Python

python - 计算每天发生的观察次数

Python - 多列上的 Lambda 函数

sorting - 无法正确排序泰坦尼克号数据集的 Cabin 值

mysql - SQL - 内连接表上的复合索引?

python - scrapy 中的 Djangoitem

python - 尝试制作列表字典时如何修复错误 "unhashable type: ' list'"?

python - 在 Keras 中,如何任意调整一维张量的大小?

python - 我的 css 和图像没有在 django 中显示

arrays - 从 Google 表格中的数组中提取特定字段