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
在这里,如何将结果转换为下面的值?
RUNX3 = False
ROGDI = False
FSTL3 = False
MOCOS = False
PDCD7 = True
.
.
.
答案 0 :(得分:0)
使用f-string
来选择一个要按row["NAME"]
标量的项目系列并将其转换为bool
:
#python 3.6 +
for index, row in data.iterrows():
print(f'{index} = {bool(row["RUNX3"])}')
#python bellow 3.6
for index, row in data.iterrows():
print('{} = {}'.format(index, bool(row["RUNX3"])))
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
for index, row in data.iterrows():
print(index, bool(row["RUNX3"]))
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
对于布尔值Series
,请使用astype
:
s = data["RUNX3"].astype(bool)
print (s)
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
Name: NAME, dtype: bool
答案 1 :(得分:0)
如果版本低于3.6,则@jezrael的答案除外:
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