0

I want to merge some dataframe from csv files using for loop in python. But the result is empty. Why is that so? Here is my code.

result = pandas.DataFrame(columns = ['col_A', 'col_B'])
for i in range(0, 5):
    #col_A is integer for numbering, col_B is float in range 0 to 1
    temp = pandas.DataFrame([[0, 0.5132443], [1, 0.12436421], [2, 0.12341162]], columns = ['col_A', 'col_B'])
    result.merge(temp)
print(result) #result is empty dataframe
6
  • Have you verified temp is actually assigned to a dataframe after running? Commented May 27, 2017 at 14:20
  • This code, and this code alone, runs without error? Commented May 27, 2017 at 14:24
  • Please read how to make good reproducible pandas examples and edit your post correspondingly. Commented May 27, 2017 at 16:41
  • yes, temp is actually assigned and remains after for loop. And code is run without any error Commented May 28, 2017 at 2:50
  • The challenge I am having understanding this, is that result is not defined, and thus result.merge should cause an error. Where is result defined? Commented May 28, 2017 at 3:00

1 Answer 1

1

You need to store the result of the merge:

result = result.merge(temp)

From the (DOCS

Returns:

merged : DataFrame

The output type will the be same as ‘left’, if it is a subclass of DataFrame.

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.