0

i have two dataframe:

  df1:
     colname   value
     gender     M
     status     business
     age        60

  df2:
     colname   value
     name       Susan
     Place      Africa
     gender     F

Is there a way i can concatenate these two dataframe in a way as the expected output? I tried outer join but it doesnot work, Thank you in advance.

Note: No dataframes have always the same common attribute, and also I am trying to remove the colname and value column.

Expected output:

    gender status     age  name  Place 
0    M     business   60   0      0
1    F     0           0   Susan Africa

3 Answers 3

1

You can convert to Series with colname as index and concat:

dfs = [df1, df2]

out = pd.concat([d.set_index('colname')['value'] for d in dfs],
                axis=1, ignore_index=True).T

output:

colname gender    status  age   name   Place
0            M  business   60    NaN     NaN
1            F       NaN  NaN  Susan  Africa
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

pd.concat([df1, df2], axis=0).fillna(0).reset_index(drop=True)

    gender    status   age   name   place
0      M    business   60      0       0
1      F           0    0  Susan  Africa

The fillna will replace the NaN values with 0.

Comments

0

Below line will resolves your issue , you can use the pandas transpose function to solve this problem.

df_new = pd.concat([df1,df2])
df_new.set_index("colname").T

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.