create dataframe in loop 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: dataframe for loop
import pandas as pd
data = {'Name': ['Ankit', 'Amit', 'Aishwarya', 'Priyanka'],
'Age': [21, 19, 20, 18],
'Stream': ['Math', 'Commerce', 'Arts', 'Biology'],
'Percentage': [88, 92, 95, 70]}
df = pd.DataFrame(data, columns = ['Name', 'Age', 'Stream', 'Percentage'])
print("Given Dataframe :\n", df)
print("\nIterating over rows using iterrows() method :\n")
for index, row in df.iterrows():
print (row["Name"], row["Age"])