0

I am trying to append to a dataframe for getting a expected result like below.

How do i append it right? Currently i am not getting the correct dataframe in this for loop below Please help

Code

df = pd.DataFrame([])
no = 0
for i in range(1,10):
    no = no + 1
    tt = "ab"
    lst = [no, lst]
    df = df.append(lst)

Expected result

no tt
1  ab
2  ab
3  ab
.  .
.  .
prog
  • 1,073
  • 5
  • 17

2 Answers2

3

try:

df = pd.DataFrame()
no = 0
for i in range(1,10):
    no = no + 1
    tt = "ab"
    lst = {'no':no,'tt':tt}
    df = df.append(lst,ignore_index=True)

OR

For more efficiently just simply use pd.DataFrame():

df=pd.DataFrame({'no':range(1,10),'tt':'ab'})
Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41
1

do this modification in your provide code as I did

df = pd.DataFrame([],columns=['no','tt'])
no = 0
for i in range(1,10):
    no = no + 1
    lst = [no, 'ab']
    df_length = len(df)
    df.loc[df_length] = lst

Hope this will be helpful for you