2

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?

6
  • Could you provide a minimal example of what you want to achieve? Your snippet seems to be part of a larger code. What are the shapes of the two initial dataframes? Commented Aug 6, 2015 at 11:59
  • @chris-sc I updated the code to include the dataframes shapes. I am also making sure that the dataframes has the correct shape when entering the above method Commented Aug 6, 2015 at 12:06
  • I still struggle to see the desired result - could you show with a small dataset how you want to combine the two initial dataframes? Just append them or do some more sophisticated combination? Your function seems to take some indices and combines testset with trainset data? Commented Aug 6, 2015 at 12:15
  • @chris-sc, thanks for helping David cached my mistake! problem resolved. Commented Aug 6, 2015 at 12:21
  • Arr, could have seen that.. :) Commented Aug 6, 2015 at 12:25

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.