# Iterate over two given columns only from the dataframe for column in empDfObj[['Name', 'City']]: # Select column contents by column name using [] operator columnSeriesObj = empDfObj[column] print('Colunm Name : ', column) print('Column Contents : ', columnSeriesObj.values)
Here is what the above code is Doing:
1. We have created a list of column names, which we want to iterate over.
2. We have created a for loop to iterate over the list of column names.
3. We have selected the column contents by column name using the [] operator.
4. We have printed the column name and its contents.