df = pd.DataFrame(columns=["A", "B"]) for i in range(2): this_column = df.columns[i] df[this_column] = [i, i+1] print(df) #OUTPUT # A B #0 0 1 #1 1 2
Here is what the above code is Doing:
1. Create an empty DataFrame with two columns, A and B.
2. Iterate over the columns of the DataFrame.
3. For each column, create a list of values.
4. Assign the list of values to the column.
5. Print the DataFrame.