How to correctly append a Dataframe?

  • Context: Python 
  • Thread starter Thread starter msn009
  • Start date Start date
  • Tags Tags
    Python
Click For Summary

Discussion Overview

The discussion revolves around the correct method to append rows to a DataFrame in Python using the pandas library. Participants explore issues related to the behavior of the append function within a loop, focusing on how to ensure that previous rows are retained in the new DataFrame.

Discussion Character

  • Technical explanation

Main Points Raised

  • One participant describes a problem where the DataFrame only contains one row after each loop iteration, questioning how to ensure that all previous rows are included.
  • Another participant suggests that the code may be incorrectly appending the entire function rather than the intended row.
  • A third participant proposes a correction to the code, indicating that the append operation should modify df2 directly instead of creating a new DataFrame df3.
  • One participant claims to have found a solution related to the placement of the append function, although the specifics of the solution are not detailed.
  • Another participant emphasizes the importance of sharing solutions in the thread to assist others who may be facing similar issues.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the exact nature of the problem initially, but there is agreement on the importance of correctly using the append method. The discussion includes both proposed corrections and personal solutions without resolving the initial confusion.

Contextual Notes

There are limitations regarding the clarity of the code snippets provided and the specific definitions of functions like check_function and calc_function_1, which are not fully explained. The discussion does not resolve the underlying assumptions about the DataFrame's structure or the expected output.

msn009
Messages
53
Reaction score
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
 
Technology news on Phys.org
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
 
msn009 said:
df3 = df2.append(calc_function_1)
print df3

Shouldn't that read like this?
df2 = df2.append(calc_function_1)
print df2
 
yes, i found the solution. It was the way i was appending the location of the .append which was not correct.
 
msn009 said:
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.
 
sure. i was going to update the thread as i just found the solution. thanks.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K