Python How to correctly append a Dataframe?

  • Thread starter Thread starter msn009
  • Start date Start date
  • Tags Tags
    Python
Click For Summary
The discussion revolves around a coding issue involving the appending of rows to a DataFrame in Python using pandas. The original poster faced a problem where only one row was being populated in the new DataFrame during each loop iteration. The confusion stemmed from the incorrect placement of the append function, which was appending the entire function instead of the intended row. The solution involved correctly using the append method to add the calculated row to the DataFrame. The poster acknowledged finding the solution and indicated the intention to update the thread to assist others who might face a similar issue.
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.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
7
Views
2K
  • · 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