Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Concate two dataframes by column

I have a problem with displaying my data in a correct format. What I want to do is display them side by side

But when Im doing

gh = pd.concat([data[0], data[1]], keys=["Berlin", "London"], axis=1)

I get

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

                   London               Berlin
                    val1 val2  val3      val1 val2  val3
                    mean mean  mean      mean  mean  mean
name      date
Berlin    2021-01    NaN  NaN   NaN     -3.13  0.11  4.42
          2021-02    NaN  NaN   NaN     -4.12  0.03  4.33
          2021-03    NaN  NaN   NaN      1.81  0.03  4.66

London    2021-01  -1.52  0.0  6.88       NaN   NaN   NaN
          2021-02  -2.20  0.0  7.44       NaN   NaN   NaN
          2021-03   3.16  0.0  7.05       NaN   NaN   NaN

The data is correct but this should look like this

             London              Berlin
             val1 val2  val3     val1 val2  val3
             mean mean  mean     mean  mean  mean
   date
   2021-01  -1.52  0.0  6.88     -3.13  0.11  4.42
   2021-02  -2.20  0.0  7.44     -4.12  0.03  4.33
   2021-03   3.16  0.0  7.05      1.81  0.03  4.66

What can I do to get the data in the correct format?

>Solution :

You can try DataFrame.xs to select data at a particular level of a MultiIndex

gh = pd.concat([data[0].xs("Berlin"), data[1].xs("London")], keys=["Berlin", "London"], axis=1)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading