Mohamed Ali JAMAOUI
Mohamed Ali JAMAOUI

Reputation: 14689

Create pandas dataFrame from two other using append

I am preparing a dataframe from two other dataframes so I later can use to_csv to export the results into csv file ,but the resulting dataframe is empty despite that the input row of datatype <class 'pandas.core.series.Series'> isn't empty, here is how I am doing that:

def writeCSVResult(self, indices, test_set, train_set, output_csvfile_name):
        all_columns = train_set.columns.values 

        results = pd.DataFrame(columns=all_columns)

        for i in xrange(self.testSet_length):
            results.append(test_set.loc[i],ignore_index=True)

            for j in xrange(self.numbe_of_cl):
                results.append(train_set.loc[indices[j]],ignore_index=True)

        print results.shape
        results.to_csv(output_csvfile_name, cols=all_columns, index=False)

        return 

the resulting dataframe shape (0, 20)

Further infos:

test_set shape (10,20) 
train_set shape (500000,20) 

what am I missing?

Upvotes: 2

Views: 86

Answers (1)

David Hammen
David Hammen

Reputation: 33106

pandas.DataFrame.append(other) "Appends rows of other to the end of this frame, returning a new object."

In other words, it does not change the object at hand. You are calling append and dropping the result on the bit floor. If you want to append to the frame in the sense of list.append, you need to use something like

results = results.append(test_set.loc[i],ignore_index=True)

Upvotes: 1

Related Questions