How to correctly append a Dataframe?

  • Python
  • Thread starter msn009
  • Start date
  • Tags
    python
  • #1
53
6

Main Question or Discussion Point

I have code snippet as below and when i run it, the dataframe seems to append after every loop and does not add the previous row into the frame, so at each time only one row is populated into the new dataframe. How do I ensure that the append function adds the previous row as well?

Python:
    df2=pd.DataFrame()

    for i,row in df.iterrows():
        c =check_function(row)

        if check_function != 'No':
            calc_function= calc_function_1(row, c)
            df3 = df2.append(calc_function_1)
            print df3
 

Answers and Replies

  • #2
phinds
Science Advisor
Insights Author
Gold Member
2019 Award
15,935
5,646
I'm not familiar w/ the dataframe object, but it looks to me like

calc_function= calc_function_1(row, c)
df3 = df2.append(calc_function_1)

is defining "calc_function" to be a row out of calc_function_1 that you want to append to df2 but instead of appending it, you then append the entire calc_function_1
 
  • #3
8,271
5,100
df3 = df2.append(calc_function_1)
print df3
Shouldn't that read like this?
df2 = df2.append(calc_function_1)
print df2
 
  • #4
53
6
yes, i found the solution. It was the way i was appending the location of the .append which was not correct.
 
  • #5
8,271
5,100
yes, i found the solution. It was the way i was appending the location of the .append which was not correct.
Please remember that it's just ordinary politeness when you find your own answer, to post it on the thread so that others stop trying to help.
 
  • #6
53
6
sure. i was going to update the thread as i just found the solution. thanks.
 

Related Threads on How to correctly append a Dataframe?

Replies
8
Views
778
Replies
1
Views
589
Replies
8
Views
6K
Replies
2
Views
2K
Replies
1
Views
524
Replies
5
Views
2K
Replies
4
Views
962
Replies
9
Views
15K
Replies
21
Views
1K
Replies
1
Views
389
Top