Reputation: 45
How can I create a new dataframe using pandas of 1000 length and assign values using for loop. I tried this way. But it doesn't work.
f = {'ID': [],'CSE':[], 'Course Name':[]}
ff = pd.DataFrame(data=f)
for i in range(1000):
ff.loc['173'] = [151, 'CSE']
It gives output like-
*ID *CSE Course Name*
173 151 CSE
Upvotes: 2
Views: 979
Reputation: 863301
Use:
for i in range(1000):
ff.loc[i] = ['173', 151, 'CSE']
Better and faster solution is create list of lists and then Dataframe
by contructor:
#loop
L = []
for i in range(10):
L.append(['173', 151, 'CSE'])
#list comprehension
#L = [['173', 151, 'CSE'] for i in range(10)]
ff = pd.DataFrame(data=L, columns=['ID','CSE','Course Name'])
print (ff.head())
ID CSE Course Name
0 173 151 CSE
1 173 151 CSE
2 173 151 CSE
3 173 151 CSE
4 173 151 CSE
Upvotes: 1