1

I have two or more dataframes in Pandas in a list

A Value1
1 ABC
2 HYT
3 IUF

A Value2
1 IJT
2 GFH
3 QER

When using

df = pd.concat(dfs, axis = 0)

And after writing to CSV, the final output is like this

A Value1 Value2
1 ABC
1        IJT
2 HYT
2        GFH
3 IUF
3        QER

But I'd like it to be like this

A Value1 Value2
1 ABC    IJT
2 HYT    GFH
3 IUF    QER

Can someone point me in the right direction?

1
  • You need to concat on axis = 1 Commented Mar 2, 2018 at 17:05

3 Answers 3

1

Two problems:

  1. Concatenating on axis 0 (should be axis 1).
  2. Having two copies of column 'A'. You can just single out 'Value2' in df2.

So:

df1, df2 = dfs
pd.concat((df1, df2['Value2']), axis=1)

returns

   A Value1 Value2
0  1    ABC    IJT
1  2    HYT    GFH
2  3    IUF    QER
Sign up to request clarification or add additional context in comments.

Comments

0

Here is one way. The benefit of this method is it works for arbitrary lst, provided each dataframe has column 'A'.

lst = [df1, df2]

df = pd.concat([i.set_index('A') for i in lst], axis=1).reset_index()

#    A Value1 Value2
# 0  1    ABC    IJT
# 1  2    HYT    GFH
# 2  3    IUF    QER

1 Comment

Thanks for the help. When I run this it looks like columns with matching names are repeating. For example, if there's another Value1 column in a later DataFrame then the value won't slot into it's relevant A index in the first Value1 column, rather it will make a new column called Value1.
0

You could achieve the desired result using pandas.merge or pandas.concat

import pandas as pd

df1 = pd.DataFrame({"A": [1, 2, 3], 
"Value1": ['ABC', 'HYT', 'IUF']})

df2 = pd.DataFrame({"A": [1, 2, 3], 
"Value2": ['IJT', 'GFH', 'QER']})

df_merge= pd.merge(df1, df2, how='left', on=["A"])

df_concat = pd.concat([df1, df2['Value2']], axis = 1)

print(df_merge)
print(df_concat)

Expected Output for pandas.merge:

   A Value1 Value2
0  1    ABC    IJT
1  2    HYT    GFH
2  3    IUF    QER

Expected Output for pandas.concat:

   A Value1 Value2
0  1    ABC    IJT
1  2    HYT    GFH
2  3    IUF    QER

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.