How to correctly append a Dataframe?

  • Python
  • Thread starter msn009
  • Start date
  • Tags
    Python
In summary, the conversation discusses a code snippet where a dataframe does not properly append rows and the person asks for help in ensuring the append function adds the previous row as well. They eventually find the solution by correcting the location of the .append function and are reminded to share their solution with others.
  • #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
 
Technology news on Phys.org
  • #2
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
msn009 said:
df3 = df2.append(calc_function_1)
print df3

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

1. How do I append a Dataframe to an existing one in Python?

To append a Dataframe to an existing one in Python, you can use the .append() method. This method takes in the Dataframe that you want to append and adds it to the end of the existing one. For example, if you have two Dataframes df1 and df2, you can append df2 to df1 by using the code df1.append(df2).

2. Can I append multiple Dataframes at once?

Yes, you can append multiple Dataframes at once by using the .concat() method. This method takes in a list of Dataframes and combines them into one. For example, if you have three Dataframes df1, df2, and df3, you can use pd.concat([df1, df2, df3]) to append them together.

3. How do I append a Dataframe column to another Dataframe?

To append a Dataframe column to another Dataframe, you can use the .join() method. This method takes in the Dataframe that you want to append the column from and adds it as a new column to the existing one. For example, if you have two Dataframes df1 and df2, you can append the column col from df2 to df1 by using the code df1.join(df2['col']).

4. How do I control the order of the columns when appending Dataframes?

To control the order of the columns when appending Dataframes, you can use the .reindex() method. This method allows you to specify the order of the columns by passing in a list of column names. For example, if you want the columns to be in the order col1, col2, and col3, you can use the code df.reindex(columns=['col1', 'col2', 'col3']) to append the Dataframes in that order.

5. Is it possible to append Dataframes with different columns?

Yes, it is possible to append Dataframes with different columns. However, the resulting Dataframe will have NaN values for columns that do not exist in one of the Dataframes. You can use the .fillna() method to replace these NaN values with a specified value. Alternatively, you can also use the .merge() method to merge Dataframes based on a common column or index.

Similar threads

  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
4
Views
877
  • Programming and Computer Science
Replies
10
Views
993
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
5
Views
966
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
8
Views
827
Back
Top