Difficulty Merging Sequential Columns in a Dataframe with Pandas

Asked

Viewed 31 times

0

I have two dates frames, the two have exactly the same index, the difference is that one has the columns 'week 9' until 'week 30', and the other has the columns 'week 31' until 'week 53'.

Until now I used the following code but it didn’t work


import pandas as pd

parte1 = pd.read_csv("parte1.csv", encoding = "utf-8", delimiter = ";")
parte2 = pd.read_csv("parte2.csv", encoding = "utf-8", delimiter = ";")

parte3 = pd.merge(parte1, parte2, how = "left")
parte3.to_csv("dados_mesclados_.csv", encoding = "utf-8", sep = ";")

What happened is that the only thing that was merged were the titles of the columns, that is, a file was generated containing the headers 'week 9' until 'week 53', but the data are only filled in referring to Parte1 (week 9 until 30). I did other tests using instead of the 'left' parameter the parameters Inner, Outer and right. The closest thing to the result I wanted was the left parameter.

1 answer

0

You can use the Concat of pandas, passing data frames and column axis:

parte3 = pd.concat([parte1, parte2], axis=1)
  • The ordering worked, but I forgot to comment but the file contains common columns of identification and between the transition of the weeks the columns of identification, which are exactly the same in both files, were repeated. You would parameterize an exception within the Concat function to concatenate only specific columns?

  • @standardmarcelo in this case may merge your problem: parte3 = pd.merge(parte1, parte2, how='inner')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.