title: How do I horizontally concatenate pandas dataframes in python
body: <p>I have tried several different ways to horizontally concatenate DataFrame objects from the Python Data Analysis Library (PANDAS), but my attempts have failed so far.</p>
  
  <h1>Desired Output for Given Inputs:</h1>
  
  <p>I have two dataframes:<br>
  d_1:</p>
  
  <pre><code>      col2    col3
  col1                
  str1     1  1.5728
  str2     2  2.4627
  str3     3  3.6143
  </code></pre>
  
  <p>d_2:</p>
  
  <pre><code>      col2    col3
  col1              
  str1     4  4.5345
  str2     5  5.1230
  str3     6  6.1233
  </code></pre>
  
  <p>I want the final resulting dataframe to be d_1 and d_2 side-by-side:</p>
  
  <pre><code>      col2    col3    col1  col2   col3
  col1                                  
  str1     1  1.5728    str1     4  4.5345
  str2     2  2.4627    str2     5  5.1230
  str3     3  3.6143    str3     6  6.1233
  </code></pre>
  
  <h1>Creating Test Inputs:</h1>
  
  <p>Here is some code which creates the dataframes:</p>
  
  <pre><code>import pandas as pd
  
  column_headers = ["col1", "col2", "col3"]
  d_1 = dict.fromkeys(column_headers)
  d_1["col1"] = ["str1", "str2", "str3"]
  d_1["col2"] = [1, 2, 3]
  d_1["col3"] = [1.5728, 2.4627, 3.6143]
  df_1 = pd.DataFrame(d_1)
  df_1 = df_1.set_index("col1")
  print("df_1:")
  print(df_1)
  print()
  
  
  d_2 = dict.fromkeys(column_headers)
  d_2["col1"] = ["str1", "str2", "str3"]
  d_2["col2"] = [4, 5, 6]
  d_2["col3"] = [4.5345, 5.123, 6.1233]
  df_2 = pd.DataFrame(d_2)
  df_2 = df_2.set_index("col1")
  print("df_2:")
  print(df_2)
  print()
  </code></pre>
  
  <h1>Failed Attempts:</h1>
  
  <h2>Failed Solution 1</h2>
  
  <p>Outer join fails to horizontally concatenate d_1 an d_2:</p>
  
  <pre><code>merged_df = df_1.join(df_2, how='outer')
  </code></pre>
  
  <p>We get the following error message:</p>
  
  <pre><code>ValueError: columns overlap but no suffix specified: Index(['col2', 'col3'], dtype='object')
  </code></pre>
  
  <h2>Failed Solution 2:</h2>
  
  <p>Making a dictionary of dictionaries does not work:</p>
  
  <pre><code># Make a dictionary of dictionaries
  merged_d = dict()
  merged_d[1] = d_1
  merged_d[2] = d_2
  merged_df = pd.DataFrame(merged_d)
  print(merged_df)
  </code></pre>
  
  <p>The resulting DataFrame looks like this:</p>
  
  <pre><code>                             1                        2
  col1        [str1, str2, str3]       [str1, str2, str3]
  col2                 [1, 2, 3]                [4, 5, 6]
  col3  [1.5728, 2.4627, 3.6143]  [4.5345, 5.123, 6.1233]
  </code></pre>
  
  <h2>Failed Solution 3:</h2>
  
  <h3>Subattempt 3a:</h3>
  
  <p>Making a dictionary of DataFrames does not seem to work either:</p>
  
  <pre><code>merged_d = dict()
  merged_d[1] = df_1
  merged_d[2] = df_2
  merged_df = pd.DataFrame(merged_d)
  print(merged_df)
  </code></pre>
  
  <p>We get the following error message:</p>
  
  <pre><code>ValueError: If using all scalar values, you must pass an index
  </code></pre>
  
  <h3>Subattempt 3b:</h3>
  
  <p>Passing an index into the DataFrame constructor does not help much:</p>
  
  <pre><code>merged_df = pd.DataFrame(data = merged_d, index = [1, 2])
  </code></pre>
  
  <p>We get the error:</p>
  
  <pre><code>Value Error: cannot copy sequence with size 2 to array axis with dimension 3
  </code></pre>