【发布时间】:2018-03-11 04:59:50
【问题描述】:
我有 pandas.df 233 行 * 234 列,如果不是 nan,我需要评估每个单元格并返回相应的列标题,到目前为止,我写了以下内容:
#First get a list of all column names (except column 0):
col_list=[]
for column in df.columns[1:]:
col_list.append(column)
#Then I try to iterate through every cell and evaluate for Null
#Also a counter is initiated to take the next col_name from col_list
#when count reach 233
for index, row in df.iterrows():
count = 0
for x in row[1:]:
count = count+1
for col_name in col_list:
if count >= 233: break
elif str(x) != 'nan':
print col_name
代码并没有完全做到这一点,我需要更改什么才能让代码在 233 行后中断并转到下一个 col_name?
Example:
Col_1 Col_2 Col_3
1 nan 13 nan
2 10 nan nan
3 nan 2 5
4 nan nan 4
output:
1 Col_2
2 Col_1
3 Col_2
4 Col_3
5 Col_3
【问题讨论】:
标签: python pandas iterator nested-loops