how to iterate over columns of pandas dataframe

124

for (columnName, columnData) in df.iteritems():
    print('Column Name : ', columnName)
    print('Column Contents : ', columnData.values)
    
# OUTPUT
#Column Name :  ID
#Column Contents :  [1 2 3]
#Column Name :  NAME
#Column Contents :  ['John' 'James' 'Ana']
for name, values in df.iteritems():
    print('{name}: {value}'.format(name=name, value=values[0]))
for column in df:
    print(df[column])

Comments

Submit
0 Comments