How to correctly append a Dataframe?

  • #1
msn009
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
2021 Award
17,809
9,874
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
10,725
8,018
df3 = df2.append(calc_function_1)
print df3

Shouldn't that read like this?
df2 = df2.append(calc_function_1)
print df2
 
  • #4
msn009
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
10,725
8,018
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
msn009
53
6
sure. i was going to update the thread as i just found the solution. thanks.
 

Suggested for: How to correctly append a Dataframe?

Replies
9
Views
2K
  • Last Post
Replies
1
Views
988
Replies
8
Views
6K
Replies
4
Views
511
  • Last Post
Replies
1
Views
944
  • Last Post
Replies
5
Views
2K
  • Last Post
Replies
21
Views
2K
  • Last Post
Replies
3
Views
518
  • Last Post
Replies
9
Views
17K
  • Last Post
Replies
4
Views
2K
Top