How to correctly append a Dataframe?

  • #1
53
6
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
16,835
7,707
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
anorlunda
Staff Emeritus
Insights Author
9,341
6,359
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
anorlunda
Staff Emeritus
Insights Author
9,341
6,359
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
9
Views
1K
Replies
1
Views
781
Replies
8
Views
6K
O
Replies
2
Views
2K
Replies
1
Views
708
  • Last Post
Replies
3
Views
259
Replies
5
Views
2K
Replies
4
Views
2K
Replies
9
Views
16K
Replies
21
Views
2K
Top