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
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.

Suggested for: How to correctly append a Dataframe?

Replies
4
Views
930
Replies
7
Views
123
Replies
1
Views
622
Replies
2
Views
868
Replies
2
Views
692
Replies
2
Views
1K
Replies
1
Views
102
Back
Top