for loop pandas dataframe column code example
Example 1: creating data frame in python with for loop
df = pd.DataFrame(columns=["A", "B"])
for i in range(2):
this_column = df.columns[i]
df[this_column] = [i, i+1]
print(df)
Example 2: python loop through column in dataframe
for column in empDfObj[['Name', 'City']]:
columnSeriesObj = empDfObj[column]
print('Colunm Name : ', column)
print('Column Contents : ', columnSeriesObj.values)
Example 3: how to iterate through a pandas dataframe
columns = list(df)
for i in columns:
print (df[i][2])